Scene view cannot recognise movement of several ob

2019-08-26 21:11发布

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条回答
beautiful°
2楼-- · 2019-08-26 21:28

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.

查看更多
登录 后发表回答