public static T GetProjectSettings <T>() where T : ProjectSettings { Type type = typeof(T); if (!Attribute.IsDefined(type, typeof(AssetPathAttribute))) { throw new ArgumentException("Type does not have a AssetPathAttribute!"); } AssetPathAttribute assetPathMeta = Attribute.GetCustomAttribute(type, typeof(AssetPathAttribute), false) as AssetPathAttribute; T settings = AssetDatabase.LoadAssetAtPath <T>(assetPathMeta.FullPath); if (settings == null && assetPathMeta.CreateAssetIfMissing) { settings = ScriptableObject.CreateInstance(type) as T; if (!Directory.Exists(assetPathMeta.Path)) { Directory.CreateDirectory(assetPathMeta.Path); } AssetDatabase.CreateAsset(settings, assetPathMeta.FullPath); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(ImportAssetOptions.Default); } if (settings == null) { throw new NotImplementedException(); } return(settings); }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { AssetPathAttribute attribute = (AssetPathAttribute)base.attribute; float baseHeight = base.GetPropertyHeight(property, label); if (!attribute.type.IsSubclassOf(typeof(Object))) { return(baseHeight + 20.0f); } else if (property.propertyType == SerializedPropertyType.String) { return(baseHeight); } else { return(baseHeight + 20.0f); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { AssetPathAttribute attribute = (AssetPathAttribute)base.attribute; if (!attribute.type.IsSubclassOf(typeof(Object))) { //显示文件类型错误提示 EditorGUI.HelpBox(position, string.Format("{0} : AssetPathAttribute.type is not subclass of UnityEngine.GameObject", property.displayName), MessageType.Error); } else { if (property.propertyType == SerializedPropertyType.String) { //绘制属性框 property.stringValue = FileField(position, property.displayName, property.stringValue, attribute.type); } else { //显示非字符串错误提示 EditorGUI.HelpBox(position, string.Format("{0} : property type is not System.String", property.displayName), MessageType.Error); } } }