给物体赋值 只改变transform.position的某一维,访问另一个脚本的变量以及给物体用脚本动态添加属性

 

修改其position的三个值

this.transform.position = new Vector3 (0.618f, 2.71828f, 3.14149f);

但有时候我们只想修改其中某一维的值

解决方案:

this.transform.position = new Vector3 (0.61803f, this.transform.position.y, this.transform.position.z);

在Unity中不能对transform.position.x/y/z直接赋值的原因探究

访问另一个脚本的变量,函数:

工程设置,一个简单的测试案例

通过Value脚本给物体添加另外两个脚本

Value.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Value : MonoBehaviour {


	string poems = "Precious time, which cannot be recovered once lost.";
	// Use this for initialization
	void Start () {

		this.gameObject.AddComponent<PrintStrs> ();
		this.gameObject.AddComponent<deepThinking.NSprint> ();
		PrintStrs.poemStrs = poems;

	}
	
	// Update is called once per frame
	void Update () {

		this.gameObject.GetComponent<PrintStrs> ().printPoem ();

		this.gameObject.GetComponent<deepThinking.NSprint> ().printPome ();
		
	}
}

PrintStrs.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PrintStrs : MonoBehaviour {

	string GeYan = "Health is the thing that makes you feel that now is the best time of the year.";

	public static string poemStrs = "Insincerity is always weakness; " +
		"sincerity even in error is strength";
	// Use this for initialization
	void Start () {
		print (poemStrs);
		
	}
	
	// Update is called once per frame
	void Update () {


		print (GeYan);
		
	}

	public void printPoem()
	{
		print (poemStrs);
	}

}

wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

NSprint.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace deepThinking {	
	
public class NSprint : MonoBehaviour {

	
		string LiBaiPoem = "古来圣贤皆寂寞,惟有饮者留其名。\n陈王昔时宴平乐,斗酒十千恣欢谑。";
	// Use this for initialization
	void Start () {
		
			print ("人生得意须尽欢,莫使金樽空对月。");
	}
	
	// Update is called once per frame
	void Update () {
		
	}

		public void printPome()
		{
			print (LiBaiPoem);
			
		}
}
}

wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

运行效果

通过脚本动态给物体添加一些属性各修改属性的值

 

给cube加 boxcollider和Rigidbody,并修改Is Kinematic属性不打勾。

 

enable是控制GameObject上的组件激活/关闭

setactive是控制GameObject对象显示/关闭

 

Value.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Value : MonoBehaviour {

	//GameObject NSprintScript = new GameObject ();
	string poems = "Precious time, which cannot be recovered once lost.";
	// Use this for initialization
	void Start () {

		this.gameObject.AddComponent<PrintStrs> ();
		 this.gameObject.AddComponent<deepThinking.NSprint> ();
		deepThinking.NSprint NSprintScript = this.GetComponent<deepThinking.NSprint> ();


		PrintStrs.poemStrs = poems;

		this.gameObject.AddComponent<BoxCollider> ();
		this.gameObject.GetComponent<BoxCollider> ().enabled = false;

		this.gameObject.AddComponent<Rigidbody> ();

		this.gameObject.GetComponent<Rigidbody> ().isKinematic = true;
		this.gameObject.GetComponent<Rigidbody> ().useGravity = false;
		this.gameObject.GetComponent<Rigidbody> ().mass = 7.18281828f;



	}
	
	// Update is called once per frame
	void Update () {

		this.gameObject.GetComponent<PrintStrs> ().enabled = false;

		this.gameObject.GetComponent<deepThinking.NSprint> ().printPome ();



	}
}

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值