Unity-3d Day07

Component与脚本通信:

Component⽅法:
  GetComponent();
  GetComponent<Type>();
  GetComponent(“Type”);

     //!!!!!!强转必须要变量接收!!!!!!!
        //返回Component类型,用as强转
        Transform t1 = GetComponent(typeof(Transform)) as Transform;
        //使用泛型(常用方式)
        Transform t2 = GetComponent<Transform>();
        //使用名字获取
        Transform t3 = GetComponent("Transform") as Transform;



主要作⽤:获取游戏物体及其⼦物体的组件

  GetComponent 如果游戏物体有⼀个附加,则返回Type类型的组件,如果没有则为 null。
  GetComponentInChildren 返回Type类型组件,在GameObject或它的任何⼦物体使⽤深度优先搜索,仅返回激活的组件。
  GetComponents 在游戏物体返回全部Type类型组件。
  GetComponentsInChildren 在GameObject或任何它的⼦物体,返回全部Type类型组件

AddComponent⽅法:
 作⽤:给游戏物体添加组件
注意:没有RomoveComponent⽅法,删除组件⽤Object.Destroy()

SendMessage⽅法:
  SendMessage 在当前游戏物体的所有MonoBehaviour上搜索名称为 methodName的⽅法并调⽤
  SendMessageUpwards 在当前游戏物体及其⽗物体的所有MonoBehaviour中搜索名称为methodName的⽅法并调⽤
  BroadcastMessage 在当前游戏物体及其⼦物体的所有MonoBehaviour中搜索名称为methodName的⽅法并调⽤

接下来就是动画方面的Animation组件,老版本的基本上都不用了  但还是要了解一下。。
  Animation 启⽤“⾃动播放”(Play Automatically) 时将播放的默认动画
  Animations 可以从脚本访问的⼀系列动画
  Play Automatically 启动游戏时是否应⾃动播放动画?
  Animate Physics 打开时,动画将在物理循环中执⾏仅在与运动学刚体结合时有⽤
  Culling Type 确定不播放动画的时间

Animation常⽤属性:
  animatePhysics 为真时动画在物理循环中执⾏,与运动学刚体结合时有⽤。
  clip 默认的动画⽚段
  cullingType 消隐类型: AlwaysAnimate, BasedOnRenderers,
  BasedOnClipBounds, BasedOnUserBounds
  isPlaying 是否正在播放中
  localBounds Animation组件在本地空间的边界,参考Bounds类型
  playAutomatically 为真时⾃动播放
  this[string] 返回⽚段的动画状态
  wrapMode 动画循环模式

Animation构成框架:

Animation常⽤⽅法:

AnimationClip类属性:
  length 动画的⻓度,以秒计算(只读)
  frameRate 被采样的关键帧帧速率(只读)
  wrapMode 在动画状态设置使⽤的默认循环模式。
  localBounds 动画组件在本地坐标空间这个动画剪辑的也被附加。

AnimationClip类⽅法:
  SetCurve 给动画指定曲线⼀个特殊的属性
  ClearCurves 从剪辑清除所有曲线
  AddEvent 给剪辑添加动画事件
SetCurve函数:
  SetCurve(relativePath:string, type: Type, propertyName: string, curve:AnimationCurve);

  relativePath应⽤给该曲线的游戏物体的路径。 relativePath被格式化类似路 径,如"root/spine/leftArm"。如果relativePath为空,表⽰动 画剪辑附加的游戏物体。
  type 被动画的组件的类类型
  propertyName 被动画的属性的名字或路径
  curve 动画曲线

今天的东西很多,但好些都是以后用不到的,了解一下就可以了   我这也省略了不少…………

上代码:

using UnityEngine;
using System.Collections;

public class AnimationScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Animation anim = animation;
        //创建一个动画剪辑
        AnimationClip clip = new AnimationClip();
        //创建一个动画曲线
        AnimationCurve curve = AnimationCurve.Linear(0f, 1f, 5f, 100f);
        //AnimationCurve curve = new AnimationCurve();
        curve.AddKey(0f, 1f);
        curve.AddKey(1f, 5f);
        curve.AddKey(2f, 0f);

        clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
        clip.SetCurve("", typeof(Transform), "localPosition.y", curve);
        clip.SetCurve("", typeof(Transform), "localPosition.z", curve);

        AnimationEvent animEvent = new AnimationEvent();
        animEvent.time = 2f;
        animEvent.functionName = "PlayScaleAnimation";

        animation.AddClip(clip, "hehe");
        animation.Play("hehe");
    }
    
    // Update is called once per frame
    void Update () {
        

    }
    //当动画执行到插入事件位置  方法被触发
    public void PlayScaleAnimation()
    {
        animation.Play("ScaleAnimation");
    }
}
using UnityEngine;
using System.Collections;

public class CubeScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
        string name = GetComponentInChildren<AnimationScript>().name;
        Component[] coms = GetComponentsInChildren<AnimationScript>();
        foreach (var item in coms)
        {
            print(item.name);
        }
    }
    
    // Update is called once per frame
    void Update () {
        //transform.RotateAround(Vector3.zero, Vector3.back, 1000f * Time.time);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            animation.Play();
        }

        
    }
}

唉  这一天过的   要记的东西太多了   好了   打道回府 啦啦啦

这两天想写个像样点的项目  就不写小项目了   过几天写好了一起发  晚安,,世界

原文地址:https://www.cnblogs.com/little-sun/p/4382142.html