协程方法
如果调用的是协程方法,那么调用之后不会等调用的方法结束再执行;
协程方法的返回值是IEnumerator
返回参数的时候使用yield return;
协程方法的调用
StartCoroutine(method())
协程方法
如果调用的是协程方法,那么调用之后不会等调用的方法结束再执行;
协程方法的返回值是IEnumerator
返回参数的时候使用yield return;
协程方法的调用
StartCoroutine(method())
协程方法Coroutines
方法遇到协程方法时,两个方法一起执行,与普通方法不同,普通方法会等待这个普通方法执行完后继续向下进行
协程自身可以暂停
返回值:IEnumerator
返回参数: yield return
调用:在方法内使用StartCoroutine(协程名());
射线检测
UGUI 事件监听
1,拖拽
2,代码添加
3,通过实现接口
WWW
触摸事件 Easytouch
Camera
CharacterController
Mesh
Material
Animator
unity 4.x 5.x 2017
GetComponent<Rigidbody2D>() 代替 rigidbody2D
GetComponent<Rigidbody>() 代替 rigidbody
GetComponent<AudioSource>() 代替 audio
Unity 5.3:
ParticleSystem main = smokePuff.GetComponent<ParticleSystem>();
main.startColor
Unity 5.5+:
ParticleSystem.MainModule main = smokePuff.GetComponent<ParticleSystem>().main;
main.startColor
SceneManagement 代替 Application
OnLevelWasLoaded() 在 Unity 5中被弃用了。
2D/3D sound 如何设置2D/3D声音
Time类
Time.deltaTime:当前帧所占用的时间,约
1/60秒
Time.frameCount:帧数
public GameObject go;
Debug.Log(go.name);
Debug.Log(go.GetComponent<Transform>().name);
输出都是gameobject的名称cube。
协程暂停
yield return new WaitForSeconds()
创建游戏物体的三种方法Gameobject
1.new GameObject
2.GameObject.Instantiate
3.GameObject.CreatePrimitive
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AAGameObjectCeShi : MonoBehaviour {
public GameObject prefab;
// Use this for initialization
void Start () {
//第一种创建方法:new一个GameObject
GameObject go = new GameObject("Cube");
//第二种创建方法:instantiate实例化一个prefab(预制体)
GameObject.Instantiate(prefab);
//第三种创建方法:CreatePrimitive
GameObject.CreatePrimitive(PrimitiveType.Plane);
}
// Update is called once per frame
}
Time.timeScale=?;
时间缩放,时间系数
公式:
Time.deltaTime=
Time.deltaTime(0)*Time.timeScale
原一帧时间x时间系数
用途:
使所有物体停止
Time.realtimeSinceStartup:
从开始到当前时刻的时间
用途:
可用两次的差值测量某些动作的耗时
void DestroyGameObject() { Destroy(gameObject); }
void DestroyScriptInstance() { // Removes this script instance from the game object Destroy(this); }
void DestroyComponent() { // Removes the rigidbody from the game object Destroy(GetComponent<Rigidbody>()); }
oid DestroyObjectDelayed() { // Kills the game object in 5 seconds after loading the object Destroy(gameObject, 5); }
// When the user presses Ctrl, it will remove the // BoxCollider component from the game object void Update() { if (Input.GetButton("Fire1") && GetComponent<BoxCollider>()) { Destroy(GetComponent<BoxCollider>()); } }
DontDestroyOnLoad
切换场景时,原场景的物体都会被删除,使用这个会保留原场景的物体
FindObjectOfType
// Search for any object of Type GUITexture, // if found print its name, else print a message // that says that it was not found. public class ExampleClass : MonoBehaviour { void Start() { GUITexture texture = (GUITexture)FindObjectOfType(typeof(GUITexture)); if (texture) Debug.Log("GUITexture object found: " + texture.name); else Debug.Log("No GUITexture object could be found"); } }
检索所有物体的组件的名称,将符合条件的选出来。若检索到第一个符合条件的,将不会继续检索后面的
FindObjectsOfType
检索所有符合条件的组件,返回一个数组
1.位置的变化一般使用Lerp,线性插值
2.方向的变化推荐使用slerp,球形插值
球形插值与线性插值(也称为“lerp”)的区别在于, 向量被视为方向而不是空间中的点。
如何给游戏物体通过代码添加组件(通过代码添加)
关键字为:AddComponent
用法:实例名.AddComponent<组件名>;
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.AddComponent<Rigidbody>();//可以为组件
go.AddComponent<TimeCeShi>();//可以为自定义的脚本
createObject
1.new的方法
gameobject go=new gameobject(“Cube”);
2.instantiate 实例化的方法 根据prefab
public gameobject prefab
gameobject.instantiate(prefab);
3.createPrimitive 创建原始模型的方法
gameobject.createPrimitive(PrimitiveType.C);
2019.11.12 17:15
随机数Random.Range(0,10);不生成最大值
Random.Range(4,5f)
关闭协程的时候要和开启时相一致
()内如果是方法名,关闭时也要用方法名
如果是ienumerator,关闭时也要用ienumerator
两个必须相互对应
void update(){
Ray ray=new Ray(transform.position+transform.forward, transform.forward);
bool isCollider=Physics.Raycast(ray);
Bebug.Log(iscollider);
}
如何禁用和启用一个游戏物体
transform:游戏中的任何物体都要有transform组件,是用来定位的,每个游戏物体有且仅有一个,不能被移除
tag:通过tag来区分场景中的游戏物体
activeInHieaarchy:判断游戏物体是否处于激活状态
activeSelf:看物体的active属性是否被禁用,如果他的父物体被禁用,他本身的active还是True
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);//创建物体
Debug.Log(go.activeInHierarchy);//True
go.SetActive(false);
Debug.Log(go.activeInHierarchy);//False
Debug.Log(go.tag);//未设置标签输出Untagged
008添加组件 还可以添加脚本
AddComponent
AddComponent< Rigidbody>();
AddComponent< 脚本名>();
new waitForSeconds(xs);
暂停几秒
0
GameObject、Component和Object的千丝万缕的关系
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Debug.Log(go.name);
Debug.Log(go.GetComponent<Transform>().name);//两者输出的名字一样,都是Sphere