一天一个小知识 Unity [ExecuteInEditMode]
程序员文章站
2022-03-04 18:50:34
...
ExecuteInEditMode
Unity中默认情况下,脚本只有在运行的时候才被执行,加上此属性后,不运行程序,也能执行脚本。
例如 ,不运行unity,VideoPlayer组件也会播放视频
using UnityEngine;
using UnityEngine.Video;
[ExecuteInEditMode]
[RequireComponent(typeof(VideoPlayer))]
public class VideoPlayerPlayFromStreamingAssets : MonoBehaviour
{
public bool LoadFromStreamingAssets = true;
public string URL;
private void Start()
{
VideoPlayer vp = gameObject.GetComponent<VideoPlayer>();
if (vp != null)
{
vp.source = VideoSource.Url;
vp.playOnAwake = true;
vp.url = Application.streamingAssetsPath + "/" + URL.Replace(@"\", "/");
vp.Play();
}
}
}
上一篇: vscode 前端配置