跳到主要內容

發表文章

目前顯示的是 3月, 2018的文章

Unity 如何在專案讀取的時候自動執行,並且判斷某個Class是否存在

因為套件的需要,所以需要在專案載入的時候做一些程式,不難只需要如下程式,參考Unity官方網站 https://docs.unity3d.com/Manual/RunningEditorCodeOnLaunch.html using UnityEditor; using UnityEngine; [InitializeOnLoad] class MyClass { static MyClass () { EditorApplication.update += Update; } static void Update () { Debug.Log("Updating"); } } 雖然他說只會執行一次,但我實驗下來,每次進入viusal studio 回到unity就會再度執行一次 網友也提出了解決辦法 https://answers.unity.com/questions/1418351/how-to-run-a-script-once-when-a-project-is-first-l.html #if UNITY_EDITOR using UnityEngine ; using UnityEditor ; [ InitializeOnLoad ] public class AutoEditorCode : ScriptableObject { static AutoEditorCode m_Instance = null ; static AutoEditorCode () { EditorApplication . update += OnInit ; } static void OnInit () { EditorApplication . update -= OnInit ; m_Instance = FindObjectOfType < AutoEditorCode >(); if ( m_I