如何用字符串引用纹理?

时间:2014-09-25 16:06:13

标签: unity3d textures texture2d

我已将texture2设置为PlayerPrefs中的新“纹理”,如下所示:

PlayerPrefs.SetString("texture","texture2")

但是我怎样才能打开以下脚本的字符串:

PlayerPrefs.GetString("texture")

成:

mainTexture = PlayerPrefs.GetString("texture")

因此它会将“texture2”更改为纹理:

mainTexture = texture2

我没有使用变量,因为我希望保存(并加载)所选择的纹理。提前致谢!任何帮助表示赞赏! :)

2 个答案:

答案 0 :(得分:0)

通过知道其路径来加载纹理就像这样

string texture = "Assets/Resources/abc.png";
Texture2D inputTexture = (Texture2D)Resources.LoadAssetAtPath(texture, typeof(Texture2D));

但这不是你想要的 答案是根据这些问题thisthis
不要将字符串转换为纹理制作一个bool,如果是真的,那么将纹理设置为tex1如果为false,则将其设置为tex2

 textureBool=Convert.ToBoolean(PlayerPrefs.GetInt("sound")) ;
 if(textureBool)                      //when it is true it means it is mute
     mainTexture=texture2;
  else                                 //when it is false it means the speaker is on
    mainTexture=texture2;

答案 1 :(得分:0)

我已经弄清楚了:

我将PlayerPrefs更改为:

PlayerPrefs.SetString("tex","texture2")

为texture1做了一个。

然后,我创建了一个名为MuteCheck的函数,这就是我所说的内容:

public void MuteCheck()
{
    texture = PlayerPrefs.GetString ("tex");

    if (texture == "texture2") 
    {
        textureBool = false;
    }
    else if (texture == "texture1")
    {
        textureBool = true;
    }
}

之后我疯了这样的更新功能:

 void Update()
{
    if(textureBool == false)
    {
        AudioListener.pause = true;
        mainTexture = Texture2;
    }
    else if(textureBool == true)
    {
        AudioListener.pause = false;
        mainTexture = Texture1;
    }
}

现在它完美无缺! :)