【发布时间】:2017-06-20 01:41:56
【问题描述】:
我的统一游戏中有一个传送门。通过输入触发器,它应该加载一个场景。所以这段代码真的很短,这里是:
public class PortalData : EnvironmentCommonData // the data class with some information
{
public PortalData(string sceneToLoad) // set the scene in the constructor
{
SceneToLoad = sceneToLoad;
}
public string SceneToLoad { get; set; } // the scene to load when entering
}
public class PortalController : EnvironmentCommonController // the portal class
{
[SerializeField]
string sceneToLoad; // set the scene in the inspector
private PortalData data;
private void Start()
{
data = new PortalData(sceneToLoad); // instance for the data class
}
private void OnTriggerEnter(Collider col) // player enters the trigger
{
if (CheckCollision(col, data.PlayerObject)) // is the entering object the player?
LoadScene(data.SceneToLoad); // load the new scene
}
}
所以我正在寻找一种不使用检查器的聪明方法。我想让这一切都由脚本自动化。
所以我尝试设置控制器抽象并创建从该控制器继承的不同控制器类,但这并不优雅。
这里你可以看到我尝试的一个例子:
https://hastebin.com/utuqubafuz.cs
有人有想法吗?
完美的脚本应该是这样的
public class Portal : Monobehaviour
{
string sceneToLoad = "";
void OnTriggerEnter(Collider col)
{
if(col.gameObject.tag == "Player")
LoadScene(sceneToLoad);
}
}
但如果可能的话,我不想要检查员。
【问题讨论】:
-
您必须在某处创建它们。如果您已经在场景中生成了它们,只需使用检查器。如果您使用代码创建它们,那就是,您将在其中指定字符串。
-
您想从运行时生成
sceneToLoad变量的值吗?如果是这样,你的场景的名称是什么?