これは任意のスクリプトをコンパイルエラーを回避して使うためのサンプルである。
実際にVRCSDKに対しておこなう場合はVRCSDK2または3をインポートしたときに設定される以下のシンボルを使えばよいだろう
VRCSDK2 : VRC_SDK_VRCSDK2
VRCSDK3 : VRC_SDK_VRCSDK3
UDON : UDON
using UnityEngine; | |
using UnityEditor; | |
[InitializeOnLoad] | |
public class VRCSDKChecker | |
{ | |
// 他のスクリプトでVRCSDKを使う部分に | |
// #if VRCSDK ~ #endif | |
// で囲うことでVRCSDKがないことによるコンパイルエラーを防げる | |
private const string VRCSDK_SYMBOL = ";VRCSDK"; | |
// InitializeOnLoadをつけることでコンパイル後にstaticなコンストラクタが呼び出される | |
static VRCSDKChecker() | |
{ | |
if (CheckImportedVRCSDK()) | |
{ | |
Debug.Log("VRCSDK is imported"); | |
AddVRCSDKSymbol(); | |
} | |
else | |
{ | |
Debug.Log("VRCSDK is not imported"); | |
} | |
} | |
//[MenuItem("DevelopTest/CheckImportedVRCSDK")] | |
public static bool CheckImportedVRCSDK() | |
{ | |
return (AssetDatabase.FindAssets("t:folder VRCSDK").Length > 0); | |
} | |
//[MenuItem("DevelopTest/AddVRCSDK2Symbol")] | |
private static void AddVRCSDKSymbol() | |
{ | |
var symbols = GetSymbols(); | |
if (ExistVRCSDKSymbol(symbols)) return; | |
AddSymbol(symbols, VRCSDK_SYMBOL); | |
// シンボルを更新したので強制的にコンパイル実行 | |
ForceCompile(); | |
} | |
//[MenuItem("DevelopTest/DeleteVRCSDKSymbol")] | |
public static void DeleteVRCSDKSymbols() | |
{ | |
var symbols = GetSymbols(); | |
symbols = symbols.Replace(VRCSDK_SYMBOL, string.Empty); | |
PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, symbols); | |
ForceCompile(); | |
} | |
public static bool ExistVRCSDKSymbol(string symbols) | |
{ | |
return symbols.Contains(VRCSDK_SYMBOL); | |
} | |
private static void ForceCompile() | |
{ | |
EditorApplication.ExecuteMenuItem("Assets/Refresh"); | |
} | |
private static string GetSymbols() | |
{ | |
return PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup); | |
} | |
private static void AddSymbol(string symbols, string newSymbol) | |
{ | |
symbols += newSymbol; | |
PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, symbols); | |
} | |
// UI的に表示して視覚的にわかりやすくするもの | |
/* | |
#if VRCSDK | |
[MenuItem("DevelopTest/'VRCSDK is imported'")] | |
#else | |
[MenuItem("DevelopTest/'VRCSDK is not imported'")] | |
#endif | |
public static void Dummy() { } | |
*/ | |
} |
これは任意のスクリプトをコンパイルエラーを回避して使うためのサンプルである。
実際にVRCSDKに対しておこなう場合はVRCSDK2または3をインポートしたときに設定される以下のシンボルを使えばよいだろう
VRCSDK2 : VRC_SDK_VRCSDK2
VRCSDK3 : VRC_SDK_VRCSDK3
UDON : UDON