c# - 根据之前玩家的选择更改对象的颜色?
问题描述
我的 Unity 项目中有 2 个场景可供选择。要使用例如武器进入第二个场景,您必须在第一个场景中选择角色。我想根据我在角色选择场景中选择的角色来改变武器的颜色。如果一切都适用于数组,我该如何处理?这是我在角色选择场景中实现的脚本:
{
private GameObject[] characterList;
private int index;
private void Start()
{
index = PlayerPrefs.GetInt("CharacterSelected");
characterList = new GameObject[transform.childCount];
//array w players
for (int i = 0; i < transform.childCount; i++)
characterList[i] = transform.GetChild(i).gameObject;
foreach (GameObject go in characterList)
go.SetActive(false);
if (characterList[index])
characterList[index].SetActive(true);
}
public void ToggleLeft()
{
characterList[index].SetActive(false); //turning off the old character
index --;
if (index < 0)
index = characterList.Length - 1;
characterList[index].SetActive(true);
}
public void ToggleRight()
{
characterList[index].SetActive(false); //turning off the old character
index++;
if (index == characterList.Length)
index = 0;
characterList[index].SetActive(true);
}
public void GoBackToCharacter()
{
PlayerPrefs.SetInt("CharacterSelected", index);
SceneManager.LoadScene("WeaponSelect");
}
}
解决方案
您的问题与整个应用程序中的持久状态有关。我建议使用静态类在应用程序的整个生命周期(场景 1 和场景 2)中保留角色属性,并在必要时访问适用的属性。一个基本的例子如下:
public static class CharacaterClass
{
public static string Weapon {get; set; }
public static string Color {get; set; }
}
在scene1中设置角色武器(Form1)
Character.Weapon = "gun";
然后在场景2(Form2)中抓住那个角色武器
var charWeapon = Character.Weapon;
推荐阅读
- javascript - React.JS 与 Vanilla JS,性能:(快 28 倍)
- jquery - 如何获取a内部的类属性
- 通过 id
- 元素
- python - 如何使用 Selenium 获取没有类的标签
- ios - 为什么在使用 NSFetchedResultsController 和 Diffable Data Source 执行更新时会更新不可见的相同单元格?
- python - asyncio.as_completed 中协程和任务的区别
- jsf - 不使用 Spring 安全性和 jsf 自定义登录调用 JSF bean 方法
- typescript - ts-jest:模拟内部模块
- linux - 更新软件包时如何修复 GLib 错误?
- javascript - 在 r 中限制输入 selectizeInput 的组合闪亮
- django - Django:找不到带有参数'(3,'')'的'topic_posts'的反向