Skip to content

Instantly share code, notes, and snippets.

@gatosyocora
Last active May 5, 2020 09:43
Show Gist options
  • Save gatosyocora/f43f4a758a3bbb4f8cbfc07a2c8b51be to your computer and use it in GitHub Desktop.
Save gatosyocora/f43f4a758a3bbb4f8cbfc07a2c8b51be to your computer and use it in GitHub Desktop.
VRCSDKがプロジェクトに入っているか調べる。入っている場合特定のシンボルを登録し、VRCSDKがないことによるコンパイルエラーを防ぐ
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment