[SUGUI]UGUI Tools之 Transform属性一件复位归零

前言:

UGUI出来一阵子了。这几天看了下。功能还是很简陋。用习惯了NGUI。突然感觉功能太简陋了。叫我情何以堪!要做很多功能的话要自己写好多代码。

但是听说ugui的性能会比ngui要好,所以呢就研究学习下。万一以后淘汰了ngui呢?但是ugui的功能实在是简陋。故而产生了把ngui的一些功能移植出来的想法。

我也不知道有没有人写过类似的,反正呢,我自己半写半改的能让我学到很多东西,所以呢。就动手了!

最后毕竟是自己半写半改的工具集代码,就给咱工具集命个命吧。由于叫UGUI然后加上我的姓的首字母 所以呢 就叫:SUGUI 吧。

代码到时会陆续的写。慢慢的添加,转载的朋友请注明来源:www.bobsong.net

SUGUI工具集更新地址:http://pan.baidu.com/share/home?uk=974504731

随时更新,更新内容查阅:ReadMe.txt  脚本帮助请参阅博客文章。

难得周末。睡了一下,大晚上的睡不着。就说干就干。就开始了移植工作。首先先移植了个属性复位的小功能。方便属性复位。

先来一张图片:

20150201005307_

对比一下原来的清爽多了有木有?

2

 

最后代码如下:

//----------------------------------------------
//			  Transform值一键复位
// Copyright © 2012-2015 bobsong.net
//----------------------------------------------

using UnityEngine;
using UnityEditor;

[CanEditMultipleObjects]
[CustomEditor(typeof(Transform), true)]
public class SUGUITransformInspector : Editor
{
    static public SUGUITransformInspector instance;

	SerializedProperty mPos;
	SerializedProperty mRot;
	SerializedProperty mScale;

	void OnEnable ()
	{
		instance = this;
		mPos = serializedObject.FindProperty("m_LocalPosition");
		mRot = serializedObject.FindProperty("m_LocalRotation");
		mScale = serializedObject.FindProperty("m_LocalScale");
	}

	void OnDestroy () { instance = null; }

	/// <summary>
	/// 开始绘制Transform
	/// </summary>
	public override void OnInspectorGUI ()
	{
        EditorGUIUtility.labelWidth = 15;
        
		serializedObject.Update();
		DrawPosition();
		DrawRotation();
		DrawScale();

        serializedObject.ApplyModifiedProperties();
	}

    /// <summary>
    /// 绘制坐标
    /// </summary>
	void DrawPosition ()
	{
		GUILayout.BeginHorizontal();
		{
			bool reset = GUILayout.Button("P", GUILayout.Width(20f));

			EditorGUILayout.PropertyField(mPos.FindPropertyRelative("x"));
			EditorGUILayout.PropertyField(mPos.FindPropertyRelative("y"));
			EditorGUILayout.PropertyField(mPos.FindPropertyRelative("z"));

			if (reset) mPos.vector3Value = Vector3.zero;
		}
		GUILayout.EndHorizontal();
	}

    /// <summary>
    /// 绘制形变
    /// </summary>
	void DrawScale ()
	{
		GUILayout.BeginHorizontal();
		{
			bool reset = GUILayout.Button("S", GUILayout.Width(20f));

			EditorGUILayout.PropertyField(mScale.FindPropertyRelative("x"));
			EditorGUILayout.PropertyField(mScale.FindPropertyRelative("y"));
			EditorGUILayout.PropertyField(mScale.FindPropertyRelative("z"));

			if (reset) mScale.vector3Value = Vector3.one;
		}
		GUILayout.EndHorizontal();
	}

    #region 旋转个坑爹玩意......因为四元属性绘制没有原生支持
    enum Axes : int
	{
		None = 0,
		X = 1,
		Y = 2,
		Z = 4,
		All = 7,
	}

	Axes CheckDifference (Transform t, Vector3 original)
	{
		Vector3 next = t.localEulerAngles;

		Axes axes = Axes.None;

        if (Differs(next.x, original.x)) axes |= Axes.X;
		if (Differs(next.y, original.y)) axes |= Axes.Y;
		if (Differs(next.z, original.z)) axes |= Axes.Z;

		return axes;
	}

	Axes CheckDifference (SerializedProperty property)
	{
		Axes axes = Axes.None;

		if (property.hasMultipleDifferentValues)
		{
			Vector3 original = property.quaternionValue.eulerAngles;

			foreach (Object obj in serializedObject.targetObjects)
			{
				axes |= CheckDifference(obj as Transform, original);
				if (axes == Axes.All) break;
			}
		}
		return axes;
	}

	/// <summary>
	/// 绘制一个可编辑的浮动区域
	/// </summary>
	/// <param name="hidden">是否值用 -- 代替</param>
	static bool FloatField (string name, ref float value, bool hidden, GUILayoutOption opt)
	{
		float newValue = value;
		GUI.changed = false;

		if (!hidden)
		{
            newValue = EditorGUILayout.FloatField(name, newValue, opt);
		}
		else
		{
			float.TryParse(EditorGUILayout.TextField(name, "--", opt), out newValue);
		}

		if (GUI.changed && Differs(newValue, value))
		{
			value = newValue;
			return true;
		}
		return false;
	}

	/// <summary>
	/// 由于 Mathf.Approximately 太敏感.
	/// </summary>

	static bool Differs (float a, float b) { return Mathf.Abs(a - b) > 0.0001f; }

    /// <summary>
    /// 绘制旋转
    /// </summary>
	void DrawRotation ()
	{
		GUILayout.BeginHorizontal();
		{
			bool reset = GUILayout.Button("R", GUILayout.Width(20f));

			Vector3 visible = (serializedObject.targetObject as Transform).localEulerAngles;

			visible.x = WrapAngle(visible.x);
			visible.y = WrapAngle(visible.y);
			visible.z = WrapAngle(visible.z);

			Axes changed = CheckDifference(mRot);
			Axes altered = Axes.None;

			GUILayoutOption opt = GUILayout.MinWidth(30f);

			if (FloatField("X", ref visible.x, (changed & Axes.X) != 0, opt)) altered |= Axes.X;
			if (FloatField("Y", ref visible.y, (changed & Axes.Y) != 0, opt)) altered |= Axes.Y;
			if (FloatField("Z", ref visible.z, (changed & Axes.Z) != 0, opt)) altered |= Axes.Z;

			if (reset)
			{
				mRot.quaternionValue = Quaternion.identity;
			}
			else if (altered != Axes.None)
			{
				RegisterUndo("Change Rotation", serializedObject.targetObjects);

				foreach (Object obj in serializedObject.targetObjects)
				{
					Transform t = obj as Transform;
					Vector3 v = t.localEulerAngles;

					if ((altered & Axes.X) != 0) v.x = visible.x;
					if ((altered & Axes.Y) != 0) v.y = visible.y;
					if ((altered & Axes.Z) != 0) v.z = visible.z;

					t.localEulerAngles = v;
				}
			}
		}
		GUILayout.EndHorizontal();
	}

    /// <summary>
    /// 保证角在 180到-180度之间
    /// </summary>

    [System.Diagnostics.DebuggerHidden]
    [System.Diagnostics.DebuggerStepThrough]
    static public float WrapAngle(float angle)
    {
        while (angle > 180f) angle -= 360f;
        while (angle < -180f) angle += 360f;
        return angle;
    }


    /// <summary>
    /// 创建制定对象的撤消点
    /// </summary>
    static public void RegisterUndo(string name, params Object[] objects)
    {
        if (objects != null && objects.Length > 0)
        {
            UnityEditor.Undo.RecordObjects(objects, name);

            foreach (Object obj in objects)
            {
                if (obj == null) continue;
                EditorUtility.SetDirty(obj);
            }
        }
    }
    #endregion

}

好了,今天就暂时先移植这个。到时陆续的移植些功能过来。

 

本文链接:

https://www.bobsong.net/655.html
1 + 9 =
快来做第一个评论的人吧~