解析Unity中transform.positon.x不能赋值的原因

用C#实现的代码中,transform.positon的分量不能单独赋值修改,必须对position进行整体修改。这算是个常识,但是至于为什么却从来没有考虑过。


有人说是因为xyz分量是只读的。这样的解释纯属瞎猜。进入vector3里面看一下就知道了,并不是只读的。


并且Js实现的代码中是允许单独修改transform.position的分量的。


做一组对照试验

PositionTest.cs

[csharp]  view plain  copy
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public struct MyVector  
  6. {  
  7.     public float x, y, z;  
  8. }  
  9.   
  10. public class ClassVector  
  11. {  
  12.     public float x, y, z;  
  13. }  
  14.   
  15. public class PositionTest : MonoBehaviour {  
  16.   
  17.     //-----------------对照1  
  18.     Vector3 _posVector;  
  19.   
  20.     public Vector3 position_1  
  21.     {  
  22.         get  
  23.         {  
  24.             return _posVector;  
  25.         }  
  26.   
  27.         set  
  28.         {  
  29.             _posVector = value;  
  30.         }  
  31.     }  
  32.   
  33.     public Vector3 GetPosition_1()  
  34.     {  
  35.         return _posVector;  
  36.     }  
  37.     //------------------对照1 end  
  38.   
  39.     //------------------对照2  
  40.     MyVector _posMyVector;  
  41.   
  42.     public MyVector position_2  
  43.     {  
  44.         get  
  45.         {  
  46.             return _posMyVector;  
  47.         }  
  48.   
  49.         set  
  50.         {  
  51.             _posMyVector = value;  
  52.         }  
  53.     }  
  54.   
  55.     public MyVector GetPosition_2()  
  56.     {  
  57.         return _posMyVector;  
  58.     }  
  59.     //------------------对照2 end  
  60.   
  61.     //------------------对照3 & 4  
  62.     public Vector3 position_3;  
  63.     public MyVector position_4;  
  64.     //------------------对照3 & 4 end  
  65.   
  66.     //------------------对照5  
  67.     private ClassVector _posClassVector = new ClassVector();  
  68.   
  69.     public ClassVector positon_5  
  70.     {  
  71.         get  
  72.         {  
  73.             return _posClassVector;  
  74.         }  
  75.   
  76.         set  
  77.         {  
  78.             _posClassVector = value;  
  79.         }  
  80.     }  
  81.   
  82.     public ClassVector GetPosition_5()  
  83.     {  
  84.         return _posClassVector;  
  85.     }  
  86.     //------------------对照5 end  
  87. }  


Test.cs

[csharp]  view plain  copy
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class Test : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.         PositionTest posTest = gameObject.AddComponent<PositionTest>();  
  10.         posTest.position_1.x = 1;//error  
  11.         posTest.GetPosition_1().x = 1;//error  
  12.         posTest.position_2.x = 1;//error  
  13.         posTest.GetPosition_2().x = 1;//error  
  14.         posTest.position_3.x = 1;  
  15.         posTest.position_4.x = 1;  
  16.         posTest.positon_5.x = 1;  
  17.         posTest.GetPosition_5().x = 1;  
  18.     }  
  19. }  


错误类型都是一样的:



通过对比,可以发现,

1:用属性和方法返回的结构体是不能修改其字段的

2:直接访问公有的结构体是可以修改其字段的

3:用属性和方法返回的类的实例,是可以修改其字段的。


那么基本可以锁定,问题的原因在于结构体Struct。

Struct的赋值是值类型的,也就是

Struct A=new Struct();

Struct B=A;

A跟B并不是同一段内存空间,对B的修改,不会对A产生影响。

测试代码如下:

[csharp]  view plain  copy
  1. <span style="white-space:pre;"> </span>MyVector A = new MyVector();  
  2.         MyVector B = A;  
  3.   
  4.         A.x = 1;  
  5.         B.x = 2;  
  6.         Debug.Log(A.x);  
MyVector是之前声明的结构体,输出结果为1;

所以,用属性或者方法返回一个Struct类型的数据时,返回的其实是一个旧数据的值拷贝,而不是引用。那么修改这个新的Struct的字段,就不会改变我们的目标字段。

就像在刚才的测试中,我们想要修改的是A的x,我们通过属性和方法获取到的实际上是B。

再来一个实验:

[csharp]  view plain  copy
  1. Debug.Log(transform.position);  
  2. Vector3 posTemp = transform.position;  
  3. posTemp.x += 10;  
  4. Debug.Log(transform.position);  

两次打印结果是一样的。

所以这个问题就可以这么理解:代码transform.positon.get返回的只是position字段的一个拷贝,而你要修改这个拷贝是没有意义的。


到这里理解起来很简单,只是刚开始理解的时候没有锁定问题就会很绕。

总结一下导致这个问题的原因:

1,Transform中的position是属性(property)(换成方法也一样,因为属性的实现本质上还是方法)而不是公有字段(fiel)

2,position的类型是Vector3的,而Vector3是Struct类型

3,Struct之间的赋值是拷贝而不是引用



如果有哪里理解的不对的,欢迎指正^_^






版权声明:Hello光头原创,欢迎交流拍砖扔鸡蛋,qq:1009570451 http://blog.csdn.net/u010133610/article/details/70759076
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值