Scene view cannot recognise movement of several objects in the editor until objects are selected

匆匆过客 提交于 2019-12-24 21:25:48

问题


I created a game object and added a SpriteStuff script to it that I created with a various properties and functions for the game object. I also made a few copies of the object. After, I made a GroupSpriteStuff game object which has the following property

public List<SpriteStuff> spriteStuffs;

I added an editor script for GroupSpriteStuff (GroupSpriteStuffEditor) that iterates through spriteStuffs to move each object using a slider. The movement of objects in spriteStuffs is only seen when I select the objects after moving the slider, if I don't select the objects after moving the slider the changes are aren't visible in the scene view. Below is the GroupSpriteStuffEditor:

GroupSpriteStuff groupSpriteStuff;    
float groupSpritesMvmtSliderValue = 0.0f;

void OnEnable()
{
    groupSpriteStuff = (GroupSpriteStuff)target;
}

    public override void OnInspectorGUI()
{
    base.OnInspectorGUI();

    EditorGUI.BeginChangeCheck();

    groupSpritesMvmtSliderValue = EditorGUILayout.Slider("Group Movement", groupSpriteStuff.originalGroupSpritesMvmtSliderValue, 0.0f, 1.0f);

    if (!Mathf.Approximately(groupSpriteStuff.originalGroupSpritesMvmtSliderValue, groupSpritesMvmtSliderValue))
    {
        for (int i = 0; i < groupSpriteStuff.spriteStuffs.Count; i++)
        {
            spriteStuffs[i].UseTestMovement(0.2f);
        }

        groupSpriteStuff.originalGroupSpritesMvmtSliderValue = groupSpritesMvmtSliderValue;
    }

    if (EditorGUI.EndChangeCheck())
    {

        SceneView.RepaintAll();
    }

}

How can I get the scene view to update/recognise the changes in movement I make with the slider?


回答1:


OnInspectorGUI is only called while moving in the Object's Inspector.

Instead you can use OnSceneGUI whis is repeately called while moving in the Scene View instead.

You have to separate the Inspector from the SceneView code.

OnEnable in an Editor is called when the Inspector is loaded due to selecting the object. Instead initialize your inspector with Awake:

GroupSpriteStuff groupSpriteStuff;    
float groupSpritesMvmtSliderValue = 0.0f;

void Awake()
{
    groupSpriteStuff = (GroupSpriteStuff)target;
}

private void OnSceneGUI()
{
    // to be sure you could also add it here
    groupSpriteStuff = (GroupSpriteStuff)target;

    groupSpritesMvmtSliderValue = EditorGUILayout.Slider("Group Movement", groupSpriteStuff.originalGroupSpritesMvmtSliderValue, 0.0f, 1.0f);
}

private void OnSceneView()
{
    groupSpriteStuff = (GroupSpriteStuff)target;

    if (!Mathf.Approximately(groupSpriteStuff.originalGroupSpritesMvmtSliderValue, groupSpritesMvmtSliderValue))
    {
        for (int i = 0; i < groupSpriteStuff.spriteStuffs.Count; i++)
        {
            //you were also missing groupSpriteStuff again here
            groupSpriteStuff.spriteStuffs[i].UseTestMovement(0.2f);
        }

        groupSpriteStuff.originalGroupSpritesMvmtSliderValue = groupSpritesMvmtSliderValue;
    }
}

Though be careful: you are setting the float value everytime to 0 again. That might lead to problems as well.



来源:https://stackoverflow.com/questions/52790971/scene-view-cannot-recognise-movement-of-several-objects-in-the-editor-until-obje

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!