Skip to content

Instantly share code, notes, and snippets.

@braindroplabs
Last active January 15, 2017 16:23
Show Gist options
  • Save braindroplabs/16c78b157a99370285db1c90038647f1 to your computer and use it in GitHub Desktop.
Save braindroplabs/16c78b157a99370285db1c90038647f1 to your computer and use it in GitHub Desktop.
Everything is working as intended currently except for the rounding of rotation values. I'm assuming this has something to do with Quaternions (of which I'm not quite familiar). My approach on 133-139 isn't working, any ideas as to why? What I ultimately want is to 1) Rotate a GO in the SceneView 2) When done rotating (mouse up) I want the XYZ v…
// DOCUMENTATION -----------------------------------------------------------------------------------
// This custom EditorWindow exists to streamline common level design scenarios for Shape Rain.
// DEPENDENCIES ------------------------------------------------------------------------------------
using UnityEngine;
using System;
using UnityEditor;
// DEFINITION --------------------------------------------------------------------------------------
public class ShapeRainEditorWindow : EditorWindow
{
// CACHE ---------------------------------------------------------------------------------------
private GameObject _tempSelectedObject;
private bool _isValidActiveSelection;
// INITIALIZATION ------------------------------------------------------------------------------
[MenuItem("Window/Shape Rain Editor")]
private static void Init()
{
ShapeRainEditorWindow window = (ShapeRainEditorWindow)EditorWindow.GetWindow(typeof(ShapeRainEditorWindow));
window.Show();
}
// HOOKS ---------------------------------------------------------------------------------------
private void OnEnable()
{
Selection.selectionChanged += onSelectionChanged;
SceneView.onSceneGUIDelegate += onSceneGUI;
}
private void OnDisable()
{
Selection.selectionChanged -= onSelectionChanged;
SceneView.onSceneGUIDelegate -= onSceneGUI;
}
private void OnGUI()
{
Repaint();
renderSectionHeading("Scene Camera");
renderSectionChangeSceneView(); // Scene view front/back buttons
renderSectionHeading("Active GamePiece");
renderSectionSwapGamePieceSide(); // GamePiece front/back position swap buttons
}
private void Update()
{
// Object selection
if(_tempSelectedObject != null)
{
Selection.activeObject = _tempSelectedObject;
_tempSelectedObject = null;
}
}
// METHODS -------------------------------------------------------------------------------------
private void renderSectionHeading(string heading)
{
EditorGUILayout.LabelField(heading, EditorStyles.boldLabel);
}
private void renderSectionChangeSceneView()
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Swap Camera Angle");
if(GUILayout.Button("Front"))
SceneView.lastActiveSceneView.LookAt(Vector3.zero, Quaternion.Euler(0, 0, 0));
else if(GUILayout.Button("Back"))
SceneView.lastActiveSceneView.LookAt(Vector3.zero, Quaternion.Euler(0, 180, 0));
EditorGUILayout.EndHorizontal();
}
private void renderSectionSwapGamePieceSide()
{
// Escape condition vs. render buttons
if(Selection.activeGameObject == null || _isValidActiveSelection == false)
{
EditorGUILayout.LabelField("Inactive");
return;
}
else
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Swap Gameboard Position");
if(GUILayout.Button("Front"))
updateActiveGameObjectOnGameBoard(true);
else if(GUILayout.Button("Back"))
updateActiveGameObjectOnGameBoard(false);
EditorGUILayout.EndHorizontal();
}
}
private void selectivelySelect(GameObject target)
{
// Escape condition for null parent
if(target.transform.parent == null) { return; }
// Validate or recursively climb the parent until valid
string name = target.transform.parent.name;
//TODO abstract out validation to make reusable (through the EditorWindow have a UI for customizing what validates a selectiveSelect)
bool validParent = string.Equals(name, "ActorsContainer") || string.Equals(name, "GemsContainer") || string.Equals(name, "AbilitiesContainer");
if(validParent)
{
_isValidActiveSelection = true;
_tempSelectedObject = target;
}
else
selectivelySelect(target.transform.parent.gameObject);
}
private void clampGamePiece()
{
// Update activeGameObject
GameObject activeGameObject = Selection.activeObject as GameObject;
// Escape condition
if(activeGameObject == null) { return; }
// Clamp position to two decimals
Vector3 curPos = activeGameObject.transform.position;
Vector3 newPos = new Vector3((float)Math.Round((double)curPos.x, 2), (float)Math.Round((double)curPos.y, 2), (float)Math.Round((double)curPos.z, 2));
activeGameObject.transform.position = newPos;
// Clamp rotation to 1 degree
Vector3 curRot = activeGameObject.transform.rotation.eulerAngles;
curRot.z = Mathf.Round(curRot.z);
activeGameObject.transform.eulerAngles = new Vector3(curRot.x, curRot.y, curRot.z);
// This logs the whole number values as expected, but the Transform component in the Editor still doesn't respect this. WTF?
Console.Log("z:", curRot.z, "updated:", activeGameObject.transform.rotation.eulerAngles.z);
}
private void updateActiveGameObjectOnGameBoard(bool isMoveToBack)
{
// Update activeGameObject
GameObject activeGameObject = Selection.activeObject as GameObject;
// Escape condition
if(activeGameObject == null) { return; }
// Update Back vs. Front
Vector3 curPos = activeGameObject.transform.localPosition;
Vector3 curRot = activeGameObject.transform.localRotation.eulerAngles;
bool isCollectible = activeGameObject.GetComponent<Collectible>() != null;
int zPositionOffset = isCollectible ? 3 : 1;
int yRotationOffset = isMoveToBack ? 180 : 0;
if(activeGameObject != null)
{
// Position
activeGameObject.transform.localPosition = new Vector3(curPos.x, curPos.y, isMoveToBack ? -zPositionOffset : zPositionOffset);
// Rotation
activeGameObject.transform.eulerAngles = new Vector3(curRot.x, yRotationOffset, curRot.z);
}
}
// HANDLERS ------------------------------------------------------------------------------------
private void onSelectionChanged()
{
// Update _tempSelectedObject
_tempSelectedObject = Selection.activeObject as GameObject;
// Invalidate active selection until validated in selectivelySelect
_isValidActiveSelection = false;
// Escape condition
if(_tempSelectedObject == null) { return; }
// Recursively iterate until a GamePiece is selected (Gem, Ability, or Actor)
selectivelySelect(_tempSelectedObject);
}
private void onSceneGUI(SceneView sceneView)
{
// Clamp GamePieces
Event e = Event.current;
if(e.type == EventType.MouseUp && e.button == 0) { clampGamePiece(); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment