天天看點

一天一個小知識 Unity [ExecuteInEditMode]

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

        }
    }

}