Soldier Controller script in Unite 11 intro to editor video.

Hi everyone. I’am currently writing (at least trying to write) an editor for my game and I’ve watching and searching everything about. Then I came across with the unite 11 videos and there was a editor tutorial video(The link : [http://video.unity3d.com/video/3699926/unite-11-intro-to-editor][1] )

There is soldiercontroller.cs and waypoint scripts in video but they don’t show completely these scripts. I need that at least I must complete them. First I thought the sripts are from the boot camp demo. But that script is different from this. So I’m stuck again. Can anybody give me some help here please?

The Editor script is the following.

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
	
[CustomEditor (typeof(SoldierController))]
public class SoldierControllerEditor : Editor 
{
	private SerializedObject m_Object;
	private SerializedProperty m_Speed;
	private SerializedProperty m_WaypointsCount;
	
	private static string sArraySizePath = "waypoints.Array.size";
	private static string sArrayData = "waypoints.Array.data[{0}]";
	
	public void OnEnable()
	{
		m_Object = new SerializedObject(target);
		m_Speed = m_Object.FindProperty("speed");
	}
		
	private Waypoint[] GetWaypointArray()
	{
		var arrayCount = m_Object.FindProperty(sArraySizePath).intValue;
		var transformArray = new Waypoint[arrayCount];
		
		for (var i = 0 ; i < arrayCount; i++)
			transformArray *= m_Object.FindProperty(string.Format(sArrayData , i)).objectReferenceValue as Waypoint;*
  •  return transformArray;*
    
  • }*

  • private void SetWaypoint(int index, Waypoint waypoint)*

  • {*

  •  m_Object.FindProperty(string.Format(sArrayData, index)).objectReferenceValue = waypoint;	*
    
  • }*

  • private Waypoint GetWaypointAtIndex(int index)*

  • {*

  •  return m_Object.FindProperty(string.Format(kArrayData, index)).objectReferenceValue as Waypoint;*
    
  • }*

  • private void RemoveWaypointAtIndex(int index)*

  • {*

  •  for (int i = index; i < m_WaypointsCount.intValue - 1; i++)*
    
  •  	SetWaypoint(i, GetWaypointAtIndex(i+1));*
    
  •  m_WaypointsCount.intValue--;*
    
  • }*

  • private bool CanMoveUp(int index)*

  • {*

  •  return index >= 1;*
    
  • }*

  • private bool CanMoveDown(int index)*

  • {*

  •  return index < m_WaypointsCount.intValue -1;*
    
  • }*

  • private void SwapWayPoints(int index1, int index2)*

  • {*

  •  var wayPointTemp = GetWaypointAtIndex(index1);*
    
  •  SetWaypoint(index1,GetWaypointAtIndex(index1));*
    
  •  SetWaypoint(index2,wayPointTemp);*
    
  • }*

  • public override void OnInspectorGUI()*

  • {*

  •  m_Object.Update();		*
    
  •  GUILayout.Label("Soldier Properties" , EditorStyles.boldLabel);*
    
  •  EditorGUILayout.PropertyField(m_Speed);*
    
  •  if (m_Speed.floatValue < 0)*
    
  •  	m_Speed.floatValue = 0;*
    
  •  GUILayout.Label("Waypoints", EditorStyles.boldLabel);*
    
  •  var waypoints = GetWaypointArray();*
    
  •  for (int i = 0; i < waypoints.Length;i++)*
    
  •  {*
    
  •  	GUILayout.BeginHorizontal();*
    

_ var result = EditorGUILayout.ObjectField(waypoints*, typeof(Waypoint),true) as Waypoint;_
_
if(GUI.changed)_
_
SetWaypoint(i, result);*_

* var oldEnabled = GUI.enabled;*

* GUI.enabled &= CanMoveUp(i);*
* if(GUILayout.Button(“U”, GUILayout.Width(20f)))*
* SwapWayPoints(i,i-1);*

* GUI.enabled = oldEnabled && CanMoveDown(i);*
* if(GUILayout.Button(“D”, GUILayout.Width(20f)))*
* SwapWayPoints(i,i+1);*

* GUI.enabled = oldEnabled;*
* if(GUILayout.Button(“-”, GUILayout.Width(20f)))*
* RemoveWaypointAtIndex(i);*

* GUILayout.EndHorizontal();*
* }*

* DropAreaGUI();*

* m_Object.ApplyModifiedProperties();
_
}*_

* private void DropAreaGUI()*
* {*
* var evt = Event.current;*

* var dropArea = GUILayoutUtility.GetRect(0.0f, 50.0f, GUILayout.ExpandWidth(true));*
* GUI.Box(dropArea,“Add Waypoint”);*

* switch(evt.type)*
* {*
* case EventType.DragUpdated:*
* case EventType.DragPerform:*
* if (!dropArea.Contains(evt.mousePosition))*
* break;*

* DragAndDrop.visualMode = DragAndDropVisualMode.Copy;*
* DragAndDrop.activeControlID = id;*

* if(evt.type == EventType.DragPerform)*
* {*
* DragAndDrop.AcceptDrag();*

* foreach(var draggedObject in DragAndDrop.objectReferences)*
* {*
* var go = draggedObject as GameObject;*
* if(!go)*
* continue;*

* var waypoint = go.GetComponent();*
* if(!waypoint)*
* continue;*

* AddWaypoint(waypoint);*
* }*
* DragAndDrop.activeControlID = 0;*
* }*

* Event.current.Use();*
* break;*
* }*
* }*

* private void OnSceneGUI()*
* {*
* var soldier = target as SoldierController;*
* var waypoints = soldier.waypoints.Where (x => x != null).ToArray();*

* if (waypoints.Length == 0) return;*

* var oldColor = Handles.color;*
* Handles.color = Color.green;*

* if (waypoints.Length > 1)*
* {*
* for (int i = 0 ; i < waypoints.Length - 1; i++);*
_ Handles.DrawLine(waypoints*.transform.position,waypoints[i+1].transform.position);
}
Handles.DrawLine(waypoints.First().transform.position,waypoints.Last().transform.position);
Handles.color = oldColor;*_

* Handles.BeginGUI();*
* for (int i = 0; i < waypoints.Length; i++)*
* {*
_ var guiPoint = HandleUtility.WorldToGUIPoint(waypoints*.transform.position);
var rect = new Rect(guiPoint.x - 50.0f , guiPoint.y - 40 , 100 , 20);
GUI.Box (rect , "Waypoint: " + i);
}
Handles.EndGUI();*_

* var currentPosition = soldier.transform.position;*
_ Handles.ArrowCap(0 , currentPosition, soldier.transform.rotation , soldier.speed * 0.3f);
* }
}*

And the Soldier Controller script’s first part :
using UnityEngine;
using System.Collections;_

public class SoldierController : MonoBehaviour {
* public float speed;*

* public Waypoint[] waypoints;*

* private int currentWaypointIndex;*
* private Transform currentWaypoint*
* {*
* get*
* {*
* if (currentWaypointIndex < waypoints.Length && waypoints[currentWaypointIndex] != null)*
* return waypoints[currentWaypointIndex].transform;*

* return null;*
* }*
* }*
_*[1]: http://video.unity3d.com/video/3699926/unite-11-intro-to-editor*_

I don’t know where the code is but, I was able to get this to work with another Waypoint class where the Waypoint is just a small script that is a Monobehaviour and therefore has a transform. I mean, it literally could be anything.

There are some errors above, too. As you know the video presenters kept using different, more complex code. It sure would be nice if they shared the final results we could access. Still, it was a great video. By the way: You did great getting all that code in, considering!

Some changes that might help you…

Replace:

return m_Object.FindProperty(string.Format(kArrayData, index)).objectReferenceValue as Waypoint;

with…

return m_Object.FindProperty(string.Format(sArrayData, index)).objectReferenceValue as Waypoint;

Here’s a method I made for one they didn’t show:

private void AddWaypoint(Waypoint waypoint)
{
	m_WaypointsCount.intValue++;
	SetWaypoint(m_WaypointsCount.intValue - 1, waypoint);
}

Oh, and use this swap version (I just fixed the second line to use index2 as the source):

  private void SwapWayPoints(int index1, int index2)
    {
       var wayPointTemp = GetWaypointAtIndex(index1);
       SetWaypoint(index1,GetWaypointAtIndex(index2));
       SetWaypoint(index2,wayPointTemp);
    }

Next, you’ll need a calculation for that id variable:

 int id = GUIUtility.GetControlID(FocusType.Passive); 
// FYI: Passive means things that don't get keyboard focus.	
 DragAndDrop.activeControlID = id;

Then instead of the SoldierController, I have a WaypointControl class in the OnSceneGUI():

    var soldier = target as SoldierController;

I have:

   WaypointControl waypointControl = target as WaypointControl;

   Waypoint []waypoints = waypointControl.waypoints;

   if (waypoints.Length == 0) return;

Instead of the First() and Last() stuff, I’m using normal array access too:

Handles.DrawLine(waypoints[0].transform.position,waypoints[waypoints.Length - 1].transform.position);

Hope it helps and thanks for typing in that code, too!

-m
,