Last active
March 19, 2017 16:59
-
-
Save AviiNL/1730f5fff18a955d6b8fc1084f40f90c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[RequireComponent(typeof(CameraRaycaster))] | |
public class CursorAffordance : MonoBehaviour { | |
[SerializeField] | |
Texture2D unknownCursor = null; | |
[Serializable] | |
public struct CursorLayer{ | |
public int Layer; | |
public Texture2D Cursor; | |
}; | |
[SerializeField] | |
CursorLayer[] LayerToCursor; | |
Dictionary<int, Texture2D> layerToCursor = new Dictionary<int, Texture2D>(); | |
private CameraRaycaster cameraRaycaster; | |
// Use this for initialization | |
void Start () { | |
foreach(var i in LayerToCursor) | |
{ | |
layerToCursor.Add(i.Layer, i.Cursor); | |
} | |
cameraRaycaster = GetComponent<CameraRaycaster>(); | |
cameraRaycaster.OnLayerChange += CameraRaycaster_OnLayerChange; | |
} | |
private void CameraRaycaster_OnLayerChange(int layer) | |
{ | |
if(layerToCursor.ContainsKey(layer)) | |
{ | |
Cursor.SetCursor(layerToCursor[layer], Vector2.zero, CursorMode.Auto); | |
} else | |
{ | |
Cursor.SetCursor(unknownCursor, Vector2.zero, CursorMode.Auto); | |
} | |
} | |
~CursorAffordance() | |
{ | |
cameraRaycaster.OnLayerChange -= CameraRaycaster_OnLayerChange; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEngine; | |
// TODO consider changing to a property drawer | |
[CustomEditor(typeof(CursorAffordance))] | |
public class CursorAffordanceEditor : Editor | |
{ | |
bool isLayerPrioritiesUnfolded = true; // store the UI state | |
public override void OnInspectorGUI() | |
{ | |
serializedObject.Update(); // Serialize CursorAffordance instance | |
isLayerPrioritiesUnfolded = EditorGUILayout.Foldout(isLayerPrioritiesUnfolded, "Layer Cursors"); | |
if (isLayerPrioritiesUnfolded) | |
{ | |
EditorGUI.indentLevel++; | |
{ | |
BindArraySize(); | |
BindArrayElements(); | |
} | |
EditorGUI.indentLevel--; | |
} | |
serializedObject.ApplyModifiedProperties(); // De-serialize back to cameraRaycaster (and create undo point) | |
} | |
void BindArraySize() | |
{ | |
int currentArraySize = serializedObject.FindProperty("LayerToCursor.Array.size").intValue; | |
int requiredArraySize = EditorGUILayout.IntField("Size", currentArraySize); | |
if (requiredArraySize != currentArraySize) | |
{ | |
serializedObject.FindProperty("LayerToCursor.Array.size").intValue = requiredArraySize; | |
} | |
} | |
void BindArrayElements() | |
{ | |
int currentArraySize = serializedObject.FindProperty("LayerToCursor.Array.size").intValue; | |
for (int i = 0; i < currentArraySize; i++) | |
{ | |
var prop = serializedObject.FindProperty(string.Format("LayerToCursor.Array.data[{0}].Layer", i)); | |
prop.intValue = EditorGUILayout.LayerField(string.Format("Layer {0}:", i), prop.intValue); | |
var texprop = serializedObject.FindProperty(string.Format("LayerToCursor.Array.data[{0}].Cursor", i)); | |
texprop.objectReferenceValue = EditorGUILayout.ObjectField(string.Format("Cursor {0}:", i), texprop.objectReferenceValue, typeof(Texture2D), true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment