0

I feel I am very close but for the life of me I cannot figure this out as I keep getting funky results. In the image below I am wanting to make multiple of these but I just cant figure it out:

enter image description here

As you can see that I can see in the image above I would like there to be another "Size" which I can set a number and multiple of what you see in the red box would show up.

At the moment I have a Camera_Positioning script with an Editor, a [System.Serializable] script that hold the variables and a Drawer that goes with it.

I have tried making a script that would make an array of Camera_Positioning and I also attempted something along the lines of 2D arrays but I doesn't work or I am doing something wrong. Anyone have any ideas on how to make an array of the red boxes I have?

EDIT : Here is the code that I have currently :

My Camera_Positioning and Camera_Positioning_Editor script. The Camera_Positioning is what I attach to my GameObject.

public class Camera_Positioning : MonoBehaviour {

    public CamPosLookArray[] PosLook;

}


[CanEditMultipleObjects]
[CustomEditor(typeof(Camera_Positioning))]
public class Camera_Positioning_Editor : Editor {

SerializedProperty posLook;


void OnEnable()
{
    // Setup the SerializedProperties.
    posLook = serializedObject.FindProperty("PosLook");
}

public override void OnInspectorGUI()
{
    // Set the indentLevel to 0 as default (no indent).
    EditorGUI.indentLevel = 0;
    // Update
    serializedObject.Update();

    EditorGUILayout.PropertyField(posLook.FindPropertyRelative("Array.size"));

    EditorGUILayout.BeginHorizontal();
    EditorGUILayout.LabelField("Camera Position", EditorStyles.boldLabel, GUILayout.Width(Screen.width / 2.8f));
    EditorGUILayout.LabelField("Camera Look", EditorStyles.boldLabel, GUILayout.Width(Screen.width / 2.8f));
    EditorGUILayout.LabelField("Story", EditorStyles.boldLabel, GUILayout.Width(75f));
    EditorGUILayout.EndHorizontal();

    for (int i = 0; i < posLook.arraySize; i++)
    {
        EditorGUILayout.PropertyField(posLook.GetArrayElementAtIndex(i), GUIContent.none);
    }

    // Apply.
    serializedObject.ApplyModifiedProperties();
}
}

My CamPosLookArray script:

[System.Serializable]
public class CamPosLookArray  {

public CameraPos_CameraLook[] PosLookArray;
}

My CameraPos_CameraLook and CameraPos_CameraLook_Editor script:

[System.Serializable]
public class CameraPos_CameraLook {

    public Transform CameraPosition;
    public Transform CameraLook;
    public int CameraStory;
}


[CustomPropertyDrawer(typeof(CameraPos_CameraLook))]
public class CameraPos_CameraLook_Drawer : PropertyDrawer {

// Draw the property inside the given rect
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
    // Using BeginProperty / EndProperty on the parent property means that
    // prefab override logic works on the entire property.
    EditorGUI.BeginProperty(position, label, property);

    // Draw label
    position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

    // Don't make child fields be indented
    var indent = EditorGUI.indentLevel;
    EditorGUI.indentLevel = 0;

    // Calculate rects
    Rect camPos = new Rect(position.x, position.y, Screen.width / 3f, position.height);
    Rect camLook = new Rect(position.x + Screen.width / 2.75f, position.y, Screen.width / 3f, position.height);
    Rect camStory = new Rect(position.x + (2.2f *Screen.width) / 3f, position.y, 75f, position.height);

    // Draw fields - passs GUIContent.none to each so they are drawn without labels
    EditorGUI.PropertyField(camPos, property.FindPropertyRelative("CameraPosition"), GUIContent.none);
    EditorGUI.PropertyField(camLook, property.FindPropertyRelative("CameraLook"), GUIContent.none);
    EditorGUI.PropertyField(camStory, property.FindPropertyRelative("CameraStory"), GUIContent.none);

    // Set indent back to what it was
    EditorGUI.indentLevel = indent;

    EditorGUI.EndProperty();
}

}

When I have all that I get :

enter image description here

What I am trying to achieve in the picture above is everywhere you see a triangle (2 of them) should be what is in the first picture at the top in the red box.

1
  • Edited with my current code. Commented Feb 26, 2016 at 9:20

2 Answers 2

2

You could try to have a script that has an array of Camera_Positioning.

EDIT:

public class Test: MonoBehaviour
{
    public Camera_Container [] camPos;
}

[System.Serializable]
public class Camera_Container{
    public Camera_Positioning [] camPos;
}

[System.Serializable]
public class Camera_Positioning
{
    public Transform cameraPosition;
    public Transform cameraLook;
    public int story;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that but it made a drag and drop to put that script in. Would I have to make an Editor script for this new script that has an array of Camera_Positioning as well?
See the dit, I have a class that contains an array of the Camera_Positioning. Then that class gets to be an array within a component. The layout is different to yours as I think you use some editor script.
1

After making a few tweaks to the Camera_Positioning_Editor script I managed to get the inspector to look like this:

Inspector

This is OnInspectorGUI method of the Camera_Positioning_Editor script:

public override void OnInspectorGUI()
{
    EditorGUI.indentLevel = 0;
    serializedObject.Update();

    EditorGUILayout.PropertyField(posLook
                   .FindPropertyRelative("Array.size"));

    for(int i = 0; i < posLook.arraySize; i++)
    {
        SerializedProperty item = posLook.GetArrayElementAtIndex(i);
        SerializedProperty posLookArrayProperty = 
            item.FindPropertyRelative("PosLookArray");

        EditorGUILayout.PropertyField(
            posLookArrayProperty.FindPropertyRelative("Array.size"));

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Camera Position", 
            EditorStyles.boldLabel, GUILayout.Width(Screen.width / 2.8f));
        EditorGUILayout.LabelField("Camera Look", 
            EditorStyles.boldLabel, GUILayout.Width(Screen.width / 2.8f));
        EditorGUILayout.LabelField("Story", 
            EditorStyles.boldLabel, GUILayout.Width(75f));
        EditorGUILayout.EndHorizontal();

        for(int j = 0; j < posLookArrayProperty.arraySize; j++)
        {
            EditorGUILayout.PropertyField(
                posLookArrayProperty.GetArrayElementAtIndex(j), GUIContent.none);
        }
    }

    serializedObject.ApplyModifiedProperties();
}

In this example the top Size property is the size of the array within the Camera_Positioning component. Each subsequent Size is for each CamPosLookArray.PosLookArray within the Camera_Positioning.PosLook array.

2 Comments

MotoSV, can I mail you some beer? Because this is now the third time you have come to my rescue lol <3.
Moto was there anything else you changed? Because I am not getting the same thing you have in that picture

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.