Skip to content

Instantly share code, notes, and snippets.

@pr00thmatic
Created June 17, 2019 19:24
Show Gist options
  • Save pr00thmatic/dfaf5eeb5f277ed1a94b3cf7c47b97e8 to your computer and use it in GitHub Desktop.
Save pr00thmatic/dfaf5eeb5f277ed1a94b3cf7c47b97e8 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ThingCreator : MonoBehaviour {
public GameObject prototype;
public Transform positionIndicator;
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
CreateThing(positionIndicator.position);
}
}
public void CreateThing (Vector3 pos) {
GameObject created = Instantiate(prototype);
created.transform.position = pos;
created.transform.parent = transform.parent;
}
}
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
[CustomEditor(typeof(ThingCreator))]
public class ThingCreatorEditor : Editor {
ThingCreator Target { get => (ThingCreator) target; }
// https://docs.unity3d.com/ScriptReference/EditorGUILayout.html
// https://docs.unity3d.com/ScriptReference/GUILayout.html
public override void OnInspectorGUI () {
DrawDefaultInspector();
if (GUILayout.Button("Create!")) {
Target.CreateThing(Target.positionIndicator.position);
}
if (GUI.changed && !Application.isPlaying) {
EditorUtility.SetDirty(Target);
EditorSceneManager.MarkSceneDirty(Target.gameObject.scene);
}
}
// https://docs.unity3d.com/ScriptReference/Gizmos.html
// https://docs.unity3d.com/ScriptReference/Handles.html
// https://docs.unity3d.com/ScriptReference/IMGUI.Controls.BoxBoundsHandle.html
public static void DrawGizmos (ThingCreator customTarget) {
Handles.matrix = customTarget.transform.localToWorldMatrix;
}
void OnSceneGUI () {
DrawGizmos(Target);
RaycastHit hitInfo;
Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
if (Physics.Raycast(worldRay, out hitInfo)) {
Debug.Log(hitInfo.point);
if (Event.current.type == EventType.MouseDown) {
// GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive);
// Event.current.Use();
Target.CreateThing(hitInfo.point);
}
}
Handles.color = new Color(1,0,0, 1);
Handles.DrawSolidDisc(Target.transform.position, Vector3.up, 0.5f);
if (Handles.Button(Target.transform.position, Quaternion.Euler(90, 0,0),
0.5f, 0.5f, Handles.CircleHandleCap)) {
Debug.Log("!!!!??");
}
if (GUI.changed && !Application.isPlaying) {
EditorUtility.SetDirty(Target);
EditorSceneManager.MarkSceneDirty(Target.gameObject.scene);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment