【问题标题】:Load Prefab through PlayerPrefers at new scene在新场景中通过 PlayerPrefers 加载 Prefab
【发布时间】:2016-05-09 13:56:09
【问题描述】:

在我的游戏主菜单中有一个部分供玩家选择角色的皮肤并进入游戏。

我正在努力将用户选择的“Controller Animator”和“Transform”保存在 PlayerPrefers 中,并在下一个场景“Game”中的 x-0、Y-0、z-0 处实例化它。

所有皮肤都已将其“控制器”和“变换”附加到检查器(见附图)。

只有我不能在下一个“游戏”场景中实例化它们。

在我的脚本下方选择皮肤并进入“游戏”。

using UnityEngine; 
using System.Collections;
using UnityEngine.SceneManagement;

public class Choice : MonoBehaviour {

    public RuntimeAnimatorController anim;
    public Transform Bee;
    string skinChoice = "skin";

    public void ChangeAnimator() {

        Animator animator = Bee.gameObject.GetComponent<Animator>();
        animator.runtimeAnimatorController = anim;
    }

    public void Play()
    {
        SceneManager.LoadScene ("Game");
    }
}

【问题讨论】:

标签: c# unity3d unityscript unity5 unity3d-2dtools


【解决方案1】:

您可以将预制件的名称设置为静态字符串,并在下一个游戏场景开始时引用它:

public static string skinchoice = "skin";

还有一个 Creator 类:

public class Creator : MonoBehaviour { 
    void Start(){
        if (Choice.skinchoice == "skin1") Instantiate(Bee1);
        else if (Choice.skinchoice == "skin2") Instantiate(Bee2);
        ...
    }
}

【讨论】:

  • varu,通常从不在 Unity 中使用“静态”。当然初学者不应该这样做;几乎不可能理解它在 ECS 设置中的含义。 (也与这个问题完全无关:只需使用 PlayerPrefs。)
  • 我不知道 playerprefs,但我知道它们用于不同的游戏会话,但这可能是一个更好的主意。无论如何,为什么不在 Unity 中使用静态?
猜你喜欢
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
相关资源
最近更新 更多