Unity(创建脚本)

357 阅读1分钟

#一、描述 记录第一课时,脚本的创建与使用基本的API

#二、学习记录

(一)创建一个Cube方块

(二)在cube组件上添加一个脚本,选中cube组件,在屏幕右侧有着cube的组件属性栏,点击AddComponent选择New Script输入C#文件名字创建。

(三)双击脚本进入编辑页面

(四)Test脚本

using UnityEngine;
using System.Collections;

//所有挂载在组件上的脚本都必须继承于MonoBehaviour
public class Test : MonoBehaviour {


   //如果定义public修饰词,则在组件属性栏能够供用户进行填入参数,也可以供其他脚本调用

   public int age;

   public string name;

   string data; //只能在此类中进行调用



   //初始化一些变量
   void Start () {
   	print ("输出语句"); //输出log的方法
//	  gameObject就是获取此脚本挂载到的组件对象,能够根据gameObject获取组件的属性以及控制组件
   	print("组件名称:"+gameObject.name);
   	print ("组件X坐标"+transform.position.x);
   	print ("组件Y坐标"+transform.position.y);
   	print ("组件Z坐标"+transform.position.z);
//	  Test test = GetComponent <Test>(); // 此方法是获取一个其他组件,传入一个泛型参数。就可以根据test对象调用对应组件的方法
   	print ("name :"+name);
   	print ("age :"+age);

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

属性赋值

输出结果

#三、总结

此课程学习了如何创建脚本,以及使用一些基本的api。