1.script的生命周期:
public class LifeTime : MonoBehaviour {
// Use this for initialization
void Start () //一般用来给变量赋值,只有脚本组件被激活时才能被调用
{
Debug.Log("start");
}
void Awake() //一般用来创建变量,无论脚本组件是否被激活都能被调用,但所挂对象若未被激活则不可
{
Debug.Log("Awake");
}
void FixedUpdate() //物理运算使用,如colidder rigibody
{
Debug.Log("FixedUpdate"+Time.deltaTime);
}
void LateUpdate()
{
Debug.Log("LateUpdate"+Time.deltaTime);
}
// Update is called once per frame
void Update () //非物理运算使用,如物体移动
{
Debug.Log("Update");
}
}
2.
public class DisableDostroy : MonoBehaviour {
void OnEnable() //脚本的开启(开始运行游戏时运用)
{
Debug.Log("script was Enable");
}
void OnDisable() //脚本的关闭(当把物体隐藏时调用)
{
Debug.Log("script was Disable@@@@@");
}
void OnDestroy() //脚本的销毁当结束游戏运行时调用
{
Debug.Log("script was Destroy######");
}
}
3.要控制对象组件中有勾选框的东西可以用
public class EnableComponent : MonoBehaviour {
public MeshRenderer mMesh; //需要在该脚本中拖入对象
public Collider bCold;
void Start () {
}
void Update () {
if(Input.GetKeyDown(KeyCode.Space)) //控制对象的渲染和collider的is trigger
{
mMesh.enabled = false;
bCold.isTrigger = true ;
}
}
}
3.对象的SetActive(隐藏和不隐藏)
using UnityEngine;
public class SetActive : MonoBehaviour {
public GameObject obj;
// Use this for initialization
void Start () {
gameObject.SetActive(false); //可以隐藏物体
Debug.Log("Active self:" + obj.activeSelf.ToString()); //隐藏了以后转化成字符串就是false(不活跃)
Debug.Log("Active Hiereachy:" + obj.activeInHierarchy.ToString()); 隐藏了以后转化成字符串就是false(不活跃)
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0))
{
gameObject.SetActive(false);
}
4.对材质的操作
using UnityEngine;
public class SetMaterial : MonoBehaviour {
public Material mat;
public Color init_color;
// Use this for initialization
void Start () {
init_color = mat.color;
mat.color = new Color(255, 255, 255);
}
// Update is called once per frame
void Update () {
}
void OnDisable()
{
mat.color = init_color;
Debug.Log("a");
}
}
6.两个碰撞物理组件并学会自己看API
using UnityEngine;
public class SetTrigger : MonoBehaviour {
//void OnTriggerEnter(Collider other)//首先要学会自己看API,把该脚本绑到cube1,勾cube2的is Trigger,这里other就是cube1碰撞的
// //那个物体(cube2),
// {
// Debug.Log("OnTriggerEnter");
// other.transform.position=new Vector3(0,7,0);//然后可以对other做你想做的一切碰撞后的变化,如可Destroy,位置改变
// }
void OnTriggerStay(Collider others) //每帧都调用
{
others.transform.position = new Vector3(6.36f, others.transform.position.y + 0.1f, 0.3011f);
}
7.各种变量以及类
public class Variables : MonoBehaviour
{
public bool boolValue;
public int intValue;
public float floatValue=0.1f;
public Vector2 vector2Value;
public Vector3 vector3Value;
public Quaternion quaternionValue;
public byte byteValue;
public string stringValue;
// public Test testScript;
public enum enumType
{
A,B,C
}
public enumType em ;
// Use this for initialization
void Start () {
}
[System.Serializable] //无此则不会在unity中显示下面的类里的任何东西
public class SpaceComponent //类里嵌套子类
{
public enum ShipComponentType //常说的eunm类型实际上就是一个类
{
a,
b,
c
}
public ShipComponentType type; // ShipComponentType 实例化后再unity中才能显示
///
/// </summary>
public int hitPoint = 0;
public int armor = 0;
}
public SpaceComponent spaceComponent=new SpaceComponent(); //该类实例化后才可见
void Reset()
{
floatValue = 0.3f;
}
// Update is called once per frame
void Update () {
}
}
8.可见性[SerializeField]和隐藏性[HideInspector]
public class Declaration : MonoBehaviour {
[SerializeField]
GameObject objectSerializefield; //是在Unity中本不可见的变量可见
[HideInInspector]
public GameObject publicHide; //使可见的不可见
9.值类型和引用类型的区别以及Time.delatime
using UnityEngine;
public class dealtime1 : MonoBehaviour {
Transform tran; //Transform是一种引用类型,引用的是对象的位置信息
Vector3 pos; //vector3是一种值类型,向量既有值(大小)又有方向,当然他也是一种方法
// Use this for initialization
void Start () {
pos = transform.position;
tran = transform;
}
// Update is called once per frame
void Update () {
//pos = new Vector3(transform.position.x, transform.position.y +0.01f, transform.position.z);
//pos只是一种值变量,不能使物体移动
tran.position= new Vector3(transform.position.x, transform.position.y + 0.01f/**Time.deltatime*/, transform.position.z);
//没有Time.deltatime的物体运行较不稳定
}
}
10.Collider中的is trigger是碰撞检测体,未勾选则是碰撞体,发生碰撞时有明显物理现象.勾选后
如6所言,没有碰撞的物理现象,但可以通过代码让该碰撞检测体做你想做的事
11.Rigibody中的Mass是 质量,drag是阻力,Angular Drag是旋转阻力。 Is kinematic若勾选上则物理引擎失效 Collision Detection是碰撞体检测,Discrete是不连续,当物体以高速运动时要持续检测,以免物体的碰撞没被检测到 constraint是固定坐标轴
11.Rigibody中的Mass是 质量,drag是阻力,Angular Drag是旋转阻力。 Is kinematic若勾选上则物理引擎失效 Collision Detection是碰撞体检测,Discrete是不连续,当物体以高速运动时要持续检测,以免物体的碰撞没被检测到 constraint是固定坐标轴
12..AddForce(rigibody的方法)总结:
一个变量:指明力的方向Vector3.(方向),系统会默认给出力的大小
二个变量:若想自己定义力的大小,则在一的情况下加ForeceMode.(力 冲量 加速度 。。。),接下来揣测代码
13.Message响应事件
14.Transform
(1).RIght(x轴),forward(Y轴),up(Z轴)
(2)Transformpoint(从局部坐标变换到世界坐标)
(3)InverseTransform(从世界坐标变换到局部坐标) 但都没搞懂其用法
(4)TransformDirection(从局部坐标到世界坐标变换方向)
(5)InverseTransformDirection(相反)
(6)localPosition是相对于父物体的位置
(7)学会使用Rotate RotateAround LookAt
(8)transform组件中旋转顺负逆正