Unity学习笔记(二)

吐槽

没什么好吐槽的,自己不是很差,但也不是很好,哈哈哈哈,我会变的越来越好的。

1创建游戏物体的三种方式

  1. 第一种创建方法 ——创建空的游戏物体—用GameObject
  2. 第二种 根据prefab实例化的,clone出来的,根据另外一个游戏物品什么都可以
  3. createPrimitive 创建基本的物体 默认位置在0.0.0位置—-只能是基本物体
public class ApiDemo3 : MonoBehaviour {
    public GameObject prefab;
       // Use this for initialization
       void Start () {
        //1第一种创建方法 空的游戏物体
       GameObject gg = new GameObject("cube");
        //2 第二种 根据prefab实例化的,clone出来的
        GameObject.Instantiate(prefab);
        //根据另外一个游戏物品什么都可以
        //3 createPrimitive 创建基本的物体 默认位置在0.0.0
        GameObject.CreatePrimitive(PrimitiveType.Plane);
        GameObject.CreatePrimitive(PrimitiveType.Cube);

       }

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

       }

2代码给物体添加组件

AddComponent 可以添加自己写的,也可以添加内置的//必须先获得GameObject对象
go.AddComponent();
go.AddComponent();

3禁用和开启游戏物体

tag:用标签来区分游戏物体
activelnHierarchy:是否处于激活状态
activeSelf:是否自身处于激活状态
//有时候不用这个游戏物体时候可以先不激活
将其属性用SetActive方法修改这里写图片描述

//禁用物体和启动物体先获取一个游戏物品
        Debug.Log(go.activeInHierarchy);
        go.SetActive(false);
        Debug.Log(go.activeInHierarchy);
        Debug.Log(go.tag);

4场景,游戏物品,组件

组件和游戏物体是一级关系
游戏由多个场景组成—场景由游戏物体组成—–游戏物体GameObject由组件component组成
Unity面向组件开发,游戏物体想要实现什么样的功能,只需要添加相对应的组件即可,此时会在Inspector面板中显示出来,一些属性值可以可视化的更改。
– Transform组件,决定物体的位置,旋转和缩放。
– Mesh Filter组件,选择网格。
– Box Collider组件,用来给物体添加碰撞器。
– Mesh Renderer组件,可以给物体添加材质,纹理以及渲染的方式。

5Object下有用的静态方法这里写图片描述

1)Destroy
Destroy(gameObject);
Destroy(this);摧毁物体
Destroy(rigidbody);可以移除一个组件
Destror(gameObject,5);可以等五秒后摧毁
(2) DestroyImmediate:立刻销毁
(3) DestroyOnLoad:转换场景保留这个物体不被销毁。
Donnot DestroyOnLoad(transform.gameobject);

2:寻找组件
(1),FindObjectOfType<组件名>();
eg:
Light light =FindObjectOfType(); //返回值类型就是Light 类型的
light.enabled=flase; //enabled表明是否启用某个组件。

(2),FindObjectsOfType<>
eg:
Transform[] ts= FindObjectsOfType();
foreach (Transform t in ts)
{
Debug.Log(t); //不查找未激活的游戏物体。
}

3,Instantiate:实例化

public static Object Instantiate(Object original);
public static Object Instantiate(Object original, Transform parent);
public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
Destroy Removes a gameobject, component or asset.
删除一个游戏对象、组件或资源
DestroyImmediate Destroys the object obj immediately. You are strongly recommended to use Destroy instead.
立即销毁物体obj,强烈建议使用Destroy代替。
DontDestroyOnLoad Makes the object target not be destroyed automatically when loading a new scene.
加载新场景的时候使目标对象不被自动销毁。
FindObjectOfType Returns the first active loaded object of Type type.
返回Type类型第一个激活的加载的对象。
FindObjectsOfType Returns a list of all active loaded objects of Type type.
返回Type类型的所有激活的加载的物体列
表。
Instantiate Clones the object original and returns the clone.
克隆原始物体并返回克隆物体。

6GameObject独有的静态变量和方法

activeInHierarchy:Defines whether the GameObject is active in the Scene.
activeSelf:The local active state of this GameObject. (Read Only)
isStatic:Editor only API that specifies if a game object is static.
layer:The layer the game object is in.
scene:Scene that the GameObject is part of.
tag:The tag of this game object.
transform:The Transform attached to this GameObject.

Static Methods
CreatePrimitive
Creates a game object with a primitive mesh renderer and appropriate collider.
Find //根据名字查找
Finds a GameObject by name and returns it.
FindGameObjectsWithTag //查找所有的游戏物品
Returns a list of active GameObjects tagged tag. Returns empty array if no GameObject was found.
FindWithTag //一个,第一个
Returns one active GameObject tagged tag. Returns null if no GameObject was found.

7游戏物体之间的消息传递和接收

三种方法
1**BroadcastMessage**
范围:当前物品+自己所有的孩子
作用:就是启动个广播,看哪个物体身上脚本有这个方法,然后启动这个方法,不需要知道谁去接收这个消息 只要游戏物体自身有这个方法
解释:Calls the method named methodName on every MonoBehaviour in this game object or any of its children
向该游戏物体及其子物体的所有MonoBehavior发送名字为methodName的消息,其中,parameter为methodName的参数,options决定了发送消息的选项(默认为RequireReceiver,此时如果没有一个组件进行处理就会打印一个错误~)【说是发送消息,实质上是调用MonoBehavior中名称为methodName的函数~,如果接收消息的组件中没有相应名称的函数,就不会执行函数~】
先创建一个脚本发送这个广播:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//测试广播消息的
public class ApiDemo4 : MonoBehaviour {
    public GameObject target;

    // Use this for initialization
    void Start () {
        target.BroadcastMessage("Attack",null,SendMessageOptions.DontRequireReceiver);

    }

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

    }
}

然后在另一个脚本中有这个广播脚本的方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//接收消息的方法
public class cubeDemo : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

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

    }

    void Attack()
    {
        Debug.Log(this.gameObject + "正在攻击");
    }
}

2SendMessage
范围:只是自己自身
public void SendMessage(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

3SendMessageUpwards
范围:当前物体和自己的父亲
public void SendMessageUpwards(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

得到组件的各种的方法

GetComponent
Returns the component of Type type if the game object has one attached, null if it doesn’t.
GetComponentInChildren
Returns the component of Type type in the GameObject or any of its children using depth first search.
GetComponentInParent
Returns the component of Type type in the GameObject or any of its parents.
GetComponents
Returns all components of Type type in the GameObject.
GetComponentsInChildren
Returns all components of Type type in the GameObject or any of its children.
GetComponentsInParent
Returns all components of Type type in the GameObject or any of its parents.

MonoBehaviour类

MonoBehaviour是Unity中所有脚本驱动的基类(当你使用C#作为脚本语言时)。MonoBehaviour有一些事件函数会在特定的时间被调用,
Behaviour.enabled 脚本是否禁用

MonoBehaviour 表示一个单一的行为。Unity中用户对游戏对象的操作被分割成若干个单一行为。每个单一行为都作为一个MonoBehaviour类来封装。再生成每个MonoBehaviour类的实例,并作为组件嵌入游戏对象。然后按照一定的顺序(从下到上)调用每个对象的重载方法来实现游戏对象的全部行为。

Invoke 函数
1.Invoke 函数代码 Invoke(string,float):多少秒后执行某个函数[只会调用一次]。
参数说明: String:要执行的函数的名称; Float:秒数,倒计时的时间;
InvokeRepeating(string,float,float):多少秒[第二个参数]后执行某 个函数,并且以后每隔多少秒[第三个参数]都会执行该函数一次[重复调用 N 次]。 参数说明: String:要执行的函数的名称; Float:秒数,准备时间,预热时间; Float:秒数,重复调用的间隔时间;
CancelInvoke():取消这个脚本中所有的 Invoke 调用。

总结

明天继续学习,加油

猜你喜欢

转载自blog.csdn.net/sakurakider/article/details/81047979