【发布时间】:2020-11-14 02:14:29
【问题描述】:
编辑:我想更清楚,也许我解释得不好,抱歉。 这是一个安卓游戏 关于仅切换角色图标(游戏对象)(而不是角色本身)的脚本 加载或关闭游戏时,我需要该图标保持用户选择的状态。
我可以在这个(相同的)脚本中保存和调用播放器首选项吗?
这是脚本:
using System.Collections;
使用 System.Collections.Generic; 使用 UnityEngine;
公共类角色选择:MonoBehaviour {
// referenses to controlled game objects
public GameObject avatar1, avatar2, avatar3;
// variable contains which avatar is on and active
int whichAvatarIsOn = 1;
// Use this for initialization
void Start()
{
// enable first avatar and disable another one
avatar1.gameObject.SetActive(true);
avatar2.gameObject.SetActive(false);
avatar3.gameObject.SetActive(false);
}
// public method to switch avatars by pressing UI button
public void SwitchAvatar()
{
// processing whichAvatarIsOn variable
switch (whichAvatarIsOn)
{
// if the first avatar and third is on
case 1:
// then the second avatar is on now
whichAvatarIsOn = 2;
// disable the first and third one anable the second one
avatar1.gameObject.SetActive(false);
avatar2.gameObject.SetActive(true);
avatar3.gameObject.SetActive(false);
break;
// if the second and first avatar is on
case 2:
// then the third is on now
whichAvatarIsOn = 3;
// disable the second and first one and anable the third one
avatar1.gameObject.SetActive(false);
avatar2.gameObject.SetActive(false);
avatar3.gameObject.SetActive(true);
break;
// if the third and second avatar is on
case 3:
// then the first avatar is on now
whichAvatarIsOn = 1;
// disable the second and first one and anable the third one
avatar1.gameObject.SetActive(true);
avatar2.gameObject.SetActive(false);
avatar3.gameObject.SetActive(false);
break;
}
}
}
感谢您的宝贵时间
【问题讨论】:
-
这能回答你的问题吗? Saving int in Unity using playerprefs