如何在菜单中保存设置 [英] How to save a setting in the menu

查看:171
本文介绍了如何在菜单中保存设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个静音按钮脚本(用于我的菜单/暂停屏幕),这有问题.静音/取消静音部分可以按我希望的方式工作,但是每当我死了,并且将场景从游戏"更改为菜单"时,静音按钮都会将自己重置为我在唤醒功能中所说的(显然).但是我该如何保存"静音按钮状态,所以当我死亡时,它会保持在死前设置的状态.

I've made a mute button script (for in my menu/pause screen) which has a problem. The mute/unmute part works as I want it to, however whenever I die, and change scenes from 'game' to 'menu', the mute button resets itself to what I've said in the awake function(obviously). But how can I 'save' the mute button state, so when I die it stays to what I've set it to before dying.

像这样:玩游戏(保持静音,因此带有声音)->按暂停/死键进入菜单屏幕->将按钮(切换)更改为静音"(声音是关闭)->继续播放/播放->再次按下暂停/死亡->按钮仍设置为静音".

Like this: Play the game (unmuted, so with sound) -> press pause/die to get to the menu screen -> change button(toggle) to 'mute' (so sound is off) -> continue playing/play again -> press pause/die again -> button is still set to 'mute'.

这是我的脚本(对不起,有点混乱,我是C#的新手):

This is my script (sorry it's a bit messy, I'm new to C#):

public class MuteButton : MonoBehaviour 
{
    public Texture2D Texture1;
    public Texture2D Texture2;
    public Texture2D mainTexture;
    public bool textureBool;

    public int xAs = 290;
    public int yAs = 635;
    public int xSize = 130;
    public int ySize = 130;

    public float native_width = 1080;
    public float native_height = 1920;

    public GUIStyle style = null;

    void Awake() 
    {
        mainTexture = Texture1;
        textureBool = true;
        AudioListener.pause = false;
    }

    void OnGUI() 
    {
        float rx = Screen.width / native_width;
        float ry = Screen.height / native_height;

        GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, new Vector3(rx, ry, 1)); 

        //if (GUI.Button (new Rect (50,500,50,50),  mainTexture)) 
        if (GUI.Button (new Rect (xAs, yAs, xSize, ySize), mainTexture, style))
        {

            if (textureBool)
            {
                mainTexture = Texture2;   //Swap to Texture2
                textureBool = false;
                AudioListener.pause = true;
            }
            else
            {
                mainTexture = Texture1;
                textureBool = true;
                AudioListener.pause = false;
            }
        } 
    }
}

推荐答案

如果您希望游戏对象具有持久性,则可以使用

if you want a gameObject to be persistent you can use

DontDestroyOnLoad(gameObject);

并且没有同一个游戏对象的两个副本,您应该这样做

and to not have two clones of the same game object you should do like this

public static exist=false;

Void Awake() {
if (!exist) {
    DontDestroyOnLoad(this.gameObject);
    exist = true;}
 else {
    Destroy(this.gameObject);
} 

}

一种方法是将所有选项放在类中,并作为类变量发出声音,并以不会破坏的方式放置

one way is to put all your options in class and have sound as a class variable and put in a way that it doesn't destroys

public Options option;

      void Start()    
    {
        DontDestroyOnLoad(option);
        option=option.GetComponent<Options>();
        textureBool=option.sound;  
    }

void died()    
    {
         option.sound=textureBool;
    }

像这样使用的pleyerPrefs是一种较慢但更简单的方法

a slower but simpler way is pleyerPrefs that is used like this

 void Start()    
    {
        textureBool=Convert.ToBoolean(PlayerPrefs.GetInt("sound")) ;
    }


void died()    
    {
         PlayerPrefs.SetInt("sound",Convert.ToInt32(textureBool));
         PlayerPrefs.Save();
    }

这篇关于如何在菜单中保存设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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