Unity - Repaint/Redraw 游戏对象实例化后的场景

Unity - Repaint/Redraw Scene after gameObject instantiation

Unity: 5.1.1f

语言:c#

当我在 Assets/Editor/ 文件中实例化一个游戏对象时,它不会出现在场景中,直到我 select 另一个场景的游戏对象。

我试过调用一些方法,例如:

SceneView.RepaintAll();
HandleUtility.Repaint();

但没有一个看起来有效。这就是我生成对象的方式:

public class PrefabEditor: Editor {
    void OnSceneGUI() {
        GameObject prefabInstance = Instantiate(prefab) as GameObject;

        // assign him an icon label
        Texture2D tex = EditorGUIUtility.IconContent("sv_label_0").image as Texture2D;
        Type editorGUIUtilityType = typeof(EditorGUIUtility);
        BindingFlags bindingFlags = BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic;
        object[] args = new object[] {
            prefabInstance, tex
        };
        editorGUIUtilityType.InvokeMember("SetIconForObject", bindingFlags, null, null, args);

        EditorUtility.SetDirty(prefabInstance);
    }
}

我相信您使用的是编辑器 class,而不是像

这样实例化您的游戏对象
GameObject prefabInstance = Instantiate(prefab) as GameObject;

像这样实例化你的游戏对象

GameObject prefabInstance = (GameObject) PrefabUtility.InstantiatePrefab(prefab);

并尝试使用

SceneView.lastActiveSceneView.Repaint();

希望对您有所帮助。