如何在2D游戏的统一触摸事件后使用c#脚本将sprite存储在全局变量中? [英] How to store a sprite in a global variable using c# script after touch event in unity for a 2D game?

查看:177
本文介绍了如何在2D游戏的统一触摸事件后使用c#脚本将sprite存储在全局变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个团结的新手,想将精灵存储在全局变量中,以便在发生触摸事件后在其他场景中访问.在这方面的任何想法将不胜感激.

I am new to unity and want to store a sprite in a global variable to access in other scene after touch event occurs. Any idea in this regard will be appreciated.

推荐答案

我想提出一种解决这种情况的方法.

I would like to propose an approach to solve this case.

按如下方式定义Sprite Manager类"MySpriteManager":

Define a sprite manager class "MySpriteManager" as follows:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MySpriteManager {
    private static Dictionary<string, Sprite> spriteMap = new Dictionary<string, Sprite>();

    public static void AddSprite(string key, Sprite value) {
        spriteMap.add(key, value);
    }

    public static Sprite GetSprite(string key) {
        return spriteMap[key];
    }
}

只要您想保存一个精灵实例以备将来使用,

Whenever you want to save a sprite instance for future uses,

MySpriteManager.AddSprite("my_sprite_key", my_sprite_instance);

要检索以前保存的Sprite实例,

To retrieve a previously saved sprite instance,

Sprite savedSprite = MySpriteManager.GetSprite("my_sprite_key");

希望这个想法足够清楚地说明.

Hopefully the idea is illustrated clearly enough.

这篇关于如何在2D游戏的统一触摸事件后使用c#脚本将sprite存储在全局变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆