【问题标题】:Untiy | Find can only be called from the main thread团结 | find 只能从主线程调用
【发布时间】:2015-03-23 06:37:33
【问题描述】:

当我按下我制作的按钮时,我想移动名为“Capsule”的游戏对象。 json数据此时被发送到服务器。我只是想测试按钮是否可以控制游戏对象。 这是我从现在开始编写的脚本,现在发生了错误。 请给我一些想法来解决这个问题。我要做的就是在按下按钮时控制名为“Capsule”的游戏。

using UnityEngine;
using System.Collections;
using SimpleJSON;
using UXLib;
using UXLib.Base;
using System.Collections.Generic;

public class Main : MonoBehaviour {

public int Speed;
PadController padController;
public float time;

void Start () {
    padController = PadController.Instance;
}

public void init(){

}

// Update is called once per frame
void Update () {
}

public void OnTouchStart(string name, string time){
    //ObjectMoveStart();
    Debug.Log ("TouchStart -> name : " + name + ", time : " + time);
}

public void OnTouchEnd(string name, string time){
    //ObjectMoveEnd();
    Debug.Log ("TouchEnd -> name : " + name + ", time : " + time);
}

public void ObjectMoveStart(){
    GameObject cap = GameObject.Find("Capsule");
    cap.transform.Translate(Vector3.left * 10 * 10.0f);
}
public void ObjectMoveEnd(){
    GameObject cap = GameObject.Find("Capsule");
    cap.transform.Translate(Vector3.left * 0 * 0.0f);
}


void OnGUI() {

    GUI.color = Color.white; 
    GUI.skin.label.fontSize = 30;
    GUI.skin.button.fontSize = 30;

    //GUI.Label(new Rect(20, 40, 1600, 800), ">"+logString);

    if (GUI.Button (new Rect (10, 0, 80, 40), "Type1")) {
        // padType1 선택  
        padController.Init (PadController.PAD_TYPE1);
        //Arrow Buttons
        padController.SetPosition (0, -3.5f, -2.5f);   
        padController.SetPosition (1, -6.0f, 0.0f);
        padController.SetPosition (2, -1.0f, 0.0f);
        padController.SetPosition (3, -3.5f, 2.5f);
        padController.SetVisible (0, true);
        padController.SetVisible (1, true);
        padController.SetVisible (2, true);
        padController.SetVisible (3, true);
        //Alphabet Buttons 
        padController.SetPosition (4, 4.5f, 1.5f);
        padController.SetPosition (5, 3.0f, 0.0f);
        padController.SetPosition (6, 6.0f, 0.0f);
        padController.SetPosition (7, 4.5f, -1.5f);
        padController.SetVisible (4, true);
        padController.SetVisible (5, true);
        padController.SetVisible (6, true);
        padController.SetVisible (7, true);

        padController.OnTouchStart += OnTouchStart;
        padController.OnTouchEnd += OnTouchEnd;
        //padController.OnMoveStart += ObjectMoveStart;
        //padController.OnMoveEnd += ObjectMoveEnd;
        padController.Load ();
    } 
}
}

这是发生的错误。

Find 只能从主线程调用。 加载场景时,构造函数和字段初始化程序将从加载线程中执行。 不要在构造函数或字段初始化程序中使用此函数,而是将初始化代码移至 Awake 或 Start 函数。

【问题讨论】:

  • 错误到底发生在哪里?什么是 PadController 以及如何初始化?我假设它正在另一个线程上启动?如果是这种情况,您将需要某种方式将任何使用核心 UnityEngine 类/方法的调用分派回主线程。有关如何执行此操作的示例,请参阅我对此问题的回答中的 SceneLoom 类:answers.unity3d.com/questions/662891/…

标签: multithreading unity3d gameobject


【解决方案1】:

将您的 GameObject.Find 调用移至 Start()

既然你调用的是同一个游戏对象“Capsule”,为什么不在 Start 中调用一次并引用它。

在所有方法声明之外。

GameObject cap;
void Start(){
 cap = GameObject.Find("Capsule");
}

然后删除所有其他 GameObject.Find。每次都找到相同的对象是无关紧要的。

GameObject.Find 是 UnityEngine 中最慢的调用函数。考虑通过拖动 GameObject 来引用 Inspector 中的 GameObject。

【讨论】:

    【解决方案2】:

    我在我的项目中使用了套接字,我试图移动我的名为 cap 的游戏对象。但是,连接事件是不可能的。我在下面这样决定。

    void Awake () {
        Connect();
    
        for (int i = 0; i < 4; i++) {
            players[i] = GameObject.Find("Capsule_"+i);
            players[i].renderer.enabled = false;
        }
    
    }
    
    void Start() {
        for (int i=0; i < 4; i++) {
            userNames[i] = GameObject.FindGameObjectWithTag("UserName"+i);
            //userNames[i].renderer.enabled = false; 
            userNames[i].SetActive(false);
        }
    }
    
    public void Connect() {
        connectController = UXConnectController.Instance;
        connectController.Init (HOST_UID);
        connectController.SocketOpen();
        connectController.OnReceived += OnReceived;
        connectController.OnUserAdded += OnAddUser;
    
        connectController.Join ("host", "cmd", "username"); 
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-18
      • 1970-01-01
      • 2017-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 2013-04-16
      相关资源
      最近更新 更多