Unity game framework to build 2019 (XVI-XVII) localPosition simplify and Transform reset

In the last one we have collected a small tool a screen resolution detection. Then explore it further down today.

problem

We then explored today. Whether writing UI or write GamePlay, more or less need to operate Transform.

And when I new to Unity has a very unaccustomed places. It is to transform the position, angle, zoom assignment.

For example, if only is transform.localPosition.x assignment.

Code to be written.

var localPosition = transform.localPosition;
localPosition.x = 5.0f;
transform.localPosition = localPosition;

Or write

transform.localPosition = new Vector3(5.0f,transform.localPosition.y,transform.localPosition.z)

The reason is because Vector3 is struct type. We can understand it as a value type, when it receives a copy of the object is Vector3 value, rather than assigning reference. This is a bit of C # syntax details, if you do not understand children's shoes, the proposed search.

In the Quick-Cocos2dx need only one line of code is enough.

self:setPositionX(5)

Note: This is a Lua code.

The author of it, before touching Unity, begin to run from the school to do a two-year Cocos2dx, naturally difficult to get rid of this habit. Since the principle is very simple to simply write your own.

code show as below:

#if UNITY_EDITOR
using UnityEditor;
#endif

using UnityEngine;

namespace QFramework
{
	public class TransformLocalPosImprovements
	{
#if UNITY_EDITOR
		[MenuItem("QFramework/10.Transform 赋值优化")]
#endif
		private static void GenerateUnityPackageName()
		{
			var transform = new GameObject("transform").transform;

			SetLocalPosX(transform, 5.0f);
		}

		public static void SetLocalPosX(Transform transform, float x)
		{
			var localPos = transform.localPosition;
			localPos = x;
			transform.localPosition = localPos;
		}
	}
}

Code is very easy to understand, considering the length of a method name, it will become abbreviation Position Pos.

Execution result code is shown below:
006tNc79gy1fzdi1ise58j31aa0e2n06.jpg

The result is correct.

Support Y and Z

In addition to transform support for X, Y and Z. We also support Because they also used very frequently.

code show as below:

		public static void SetLocalPosY(Transform transform, float y)
		{
			var localPos = transform.localPosition;
			localPos.y = y;
			transform.localPosition = localPos;
		}
		
		public static void SetLocalPosZ(Transform transform, float z)
		{
			var localPos = transform.localPosition;
			localPos.z = z;
			transform.localPosition = localPos;
		}

Supports XY, XZ and YZ

Of course, XY, XZ and YZ is equally in need of support. But here too, you can actually have two options:

  1. Call SetPositionX, SetPositionY, SetPositionZ
  2. Logic to achieve full

The first advantage is that the code can be reused, but each time a call is made, in fact, is a value type of copy operation. It is not recommended from a performance point of view.

The second advantage is that performance is relatively better, but will increase the amount of code.

Taken together, choose the second.

code show as below:

		public static void SetLocalPosXY(Transform transform, float x, float y)
		{
			var localPos = transform.localPosition;
			localPos.x = x;
			localPos.y = y;
			transform.localPosition = localPos;
		}
		
		public static void SetLocalPosXZ(Transform transform, float x, float z)
		{
			var localPos = transform.localPosition;
			localPos.x = x;
			localPos.z = z;
			transform.localPosition = localPos;
		}
		
		public static void SetLocalPosYZ(Transform transform, float y, float z)
		{
			var localPosition = localPos;
			localPos.y = y;
			localPos.z = z;
			transform.localPosition = localPos;
		}

Code is nothing difficult.

All code is as follows:

#if UNITY_EDITOR
using UnityEditor;
#endif

using UnityEngine;

namespace QFramework
{
	public class TransformLocalPosImprovements
	{
#if UNITY_EDITOR
		[MenuItem("QFramework/10.Transform 赋值优化")]
#endif
		private static void GenerateUnityPackageName()
		{
			var transform = new GameObject("transform").transform;

			SetLocalPosX(transform, 5.0f);
			SetLocalPosY(transform, 5.0f);
			SetLocalPosZ(transform, 5.0f);
		}

		public static void SetLocalPosX(Transform transform, float x)
		{
			var localPos = transform.localPosition;
			localPos.x = x;
			transform.localPosition = localPos;
		}
		
		public static void SetLocalPosY(Transform transform, float y)
		{
			var localPos = transform.localPosition;
			localPos.y = y;
			transform.localPosition = localPos;
		}
		
		public static void SetLocalPosZ(Transform transform, float z)
		{
			var localPos = transform.localPosition;
			localPos.z = z;
			transform.localPosition = localPos;
		}
		
		public static void SetLocalPosXY(Transform transform, float x, float y)
		{
			var localPos = transform.localPosition;
			localPos.x = x;
			localPos.y = y;
			transform.localPosition = localPos;
		}
		
		public static void SetLocalPosXZ(Transform transform, float x, float z)
		{
			var localPos = transform.localPosition;
			localPos.x = x;
			localPos.z = z;
			transform.localPosition = localPos;
		}
		
		public static void SetLocalPosYZ(Transform transform, float y, float z)
		{
			var localPos = transform.localPosition;
			localPos.y = y;
			localPos.z = z;
			transform.localPosition = localPos;
		}
	}
}

This it is our 10th examples.

We can be derived.

Transform reset

Today we explore further down then, our Transform.

We often write this logic, a Transform position, rotation, scaling values ​​are reset.

code show as below:

transform.localPosition = Vector3.zero;
transform.localScale = Vector3.one;
transform.localRotation = Quaternion.identity;

It should always be written to the code.

Method after we extract as follows:

#if UNITY_EDITOR
using UnityEditor;
#endif

using UnityEngine;

namespace QFramework
{
	public class TransformIdentity : MonoBehaviour
	{
		
#if UNITY_EDITOR
		[MenuItem("QFramework/11.Transform 归一化")]
#endif
		private static void MenuClicked()
		{
			var transform = new GameObject("transform").transform;

			Identity(transform);
		}

		/// <summary>
		/// 重置操作
		/// </summary>
		/// <param name="trans">Trans.</param>
		public static void Identity(Transform transform)
		{
			transform.localPosition = Vector3.zero;
			transform.localScale = Vector3.one;
			transform.localRotation = Quaternion.identity;
		}
	}
}

Results of the implementation of the code is correct.

Then someone will ask Identity What does it mean? Identity is actually a type of matrix, the diagonal is 1 and all the other parts are all 0, as shown below.
006tNc79gy1fzdi20inapj302b03lgle.jpg

Transform is essentially a 4x4 matrix is ​​actually, this matrix may record the position of the matrix, rotation and scaling values. Transform Identity matrix corresponding to the position is 0, scaled to 1, Rotation value Quaternion.identity such a matrix.

Here the students do not understand it does not matter, the focus is Identity method to achieve the above code.

OK, here, we can once derived.

To date, we have collected a sample of ten. Full harvest, we should collect more examples, right?

Today's content on these.

Address reprint please specify: sandals notes: liangxiegame.com

more content

Guess you like

Origin www.cnblogs.com/liangxiegame/p/12617707.html