逻辑还是挺简单的,唯一注意的是新版本的unity用的VideoClip播放视频
代码就俩
public class MoveObjCtrl : MonoBehaviour
{
void OnCollisionEnter(Collision collision)
{
string tags = collision.collider.tag;
if (tags == "A")
{
Debug.Log(Time.frameCount + ": " + collision.collider);
Main.Instance.PlayMovie(gameObject, collision.collider.gameObject);
}
else if(tags == "B")
{
}
}
}
这是可移动的碰撞物体上的代码,动态添加
主代码如下
public class Main : MonoBehaviour
{
public static Main Instance = null;
public MoveObjCtrl moveObj = null;
public GameObject boom = null;
public VideoClip videoClip;
void Start()
{
Instance = this;
videoClip = Resources.Load("movie") as VideoClip;
boom = Resources.Load("boom") as GameObject;
GameObject mobj = Instantiate(Resources.Load("moveObj")) as GameObject;
moveObj = mobj.AddComponent
}
void Update()
{
//test
if (moveObj == null)
return;
if(Input.GetKey(KeyCode.W))
{
moveObj.transform.Translate(Vector3.forward*0.1f);
}
if (Input.GetKey(KeyCode.S))
{
moveObj.transform.Translate(Vector3.back * 0.1f);
}
}
public void PlayMovie(GameObject hitObj, GameObject beHitObj)
{
Destroy(hitObj);
Destroy(beHitObj);
GameObject movieobj = Instantiate(boom, beHitObj.transform.position, Quaternion.identity);
var videoPlayer = movieobj.AddComponent
var audioSource = movieobj.AddComponent
videoPlayer.playOnAwake = true;
videoPlayer.clip = videoClip;
videoPlayer.renderMode = VideoRenderMode.MaterialOverride;
videoPlayer.targetMaterialRenderer = movieobj.GetComponent
videoPlayer.targetMaterialProperty = "_MainTex";
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
videoPlayer.SetTargetAudioSource(0, audioSource);
Destroy(movieobj, (float)videoClip.length);
}
}
需要工程代码,可以加好友