一天一个小知识Unity [ExecuteInEditMode]

it2024-01-12  61

 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(); } } }

 

最新回复(0)