Skip to content

Instantly share code, notes, and snippets.

@acoppes
Created April 10, 2019 11:09
Show Gist options
  • Save acoppes/5a1d1b9f1fca8cc75feb63e16b448154 to your computer and use it in GitHub Desktop.
Save acoppes/5a1d1b9f1fca8cc75feb63e16b448154 to your computer and use it in GitHub Desktop.
Playing with unity editor
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class TestEditor : MonoBehaviour
{
public UnityEngine.Object prefab;
public GameObject parent;
public Brush brush;
private void OnEnable()
{
SceneView.duringSceneGui += OnSceneGUI;
// Selection.selectionChanged += OnSelectionChanged;
}
private bool _selectionChanged;
private void OnSceneGUI(SceneView view)
{
var e = Event.current;
// Debug.Log(e);
var mousePosition = e.mousePosition;
if ((e.type == EventType.MouseDrag || e.type == EventType.MouseDown) && e.button == 1)
{
var position = HandleUtility.GUIPointToWorldRay (mousePosition).origin;
if (e.alt)
{
Debug.Log("ERASE!");
// delete instance
var go = HandleUtility.PickGameObject(mousePosition, true);
if (go != null && go != this.gameObject)
{
if (Selection.Contains(go))
{
Selection.activeObject = null;
}
GameObject.DestroyImmediate(go);
}
}
else
{
if (brush.CanPaint(position))
{
var instance = PrefabUtility.InstantiatePrefab(prefab, parent.transform) as GameObject;
instance.transform.position = position;
LockObject(instance, true);
// instance.hideFlags = HideFlags.NotEditable | HideFlags.HideInHierarchy;
// instance.transform.GetChild(0).hideFlags = HideFlags.NotEditable | HideFlags.HideInHierarchy;
brush.Paint(position);
}
}
e.Use();
}
void LockObject(GameObject targetObject, bool recursive)
{
targetObject.hideFlags = targetObject.hideFlags | HideFlags.NotEditable;
if (recursive)
{
foreach (Transform child in targetObject.transform)
LockObject(child.gameObject, true);
}
}
void UnlockObject(GameObject targetObject, bool recursive)
{
targetObject.hideFlags = targetObject.hideFlags & ~HideFlags.NotEditable;
if (recursive)
{
foreach (Transform child in targetObject.transform)
UnlockObject(child.gameObject, true);
}
}
}
private void OnDisable()
{
SceneView.duringSceneGui -= OnSceneGUI;
// Selection.selectionChanged -= OnSelectionChanged;
}
}
////
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class TestCustomEditor
{
private static bool _selectionChanged;
static TestCustomEditor()
{
EditorApplication.update += TestUpdate;
Selection.selectionChanged += OnSelectionChanged;
}
private static void OnSelectionChanged()
{
// return;
_selectionChanged = true;
}
private static bool ObjectIsLocked(GameObject testObject)
{
if (testObject == null)
return false;
return ((int) testObject.hideFlags & (int)HideFlags.NotEditable) != 0;
}
private static void TestUpdate()
{
// Debug.Log("Update!!");
if (_selectionChanged)
{
_selectionChanged = false;
var selectedObjects = Selection.objects;
var newSelectedObjects = new List<Object>();
for (var i = 0; i < selectedObjects.Length; i++)
{
if (ObjectIsLocked(selectedObjects[i] as GameObject))
{
Debug.Log("Removed from selection!");
continue;
}
Debug.Log("Added to selection");
newSelectedObjects.Add(selectedObjects[i]);
}
Selection.objects = newSelectedObjects.ToArray();
}
}
}
///
using UnityEditor;
using UnityEngine;
[CreateAssetMenu(menuName = "Gemserk/Brush")]
public class Brush : ScriptableObject
{
// paint per second?
public float delay;
public float distance;
private double _lastTime;
private Vector2 _lastPosition;
public bool CanPaint(Vector2 position)
{
if (EditorApplication.timeSinceStartup - _lastTime > delay)
return true;
return Vector2.SqrMagnitude(position - _lastPosition) > distance * distance;
}
public void Paint(Vector2 position)
{
_lastPosition = position;
_lastTime = EditorApplication.timeSinceStartup;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment