Skip to content

Commit

Permalink
licence, readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
v.boldyrev committed Jan 12, 2018
1 parent 92883a0 commit ffb3387
Show file tree
Hide file tree
Showing 28 changed files with 935 additions and 57 deletions.
94 changes: 56 additions & 38 deletions Assets/Editor/UnityPickers/AssetPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,16 @@ private Type AssetType

private HierarchyEntry firstVisibleEntry;

[MenuItem("Tools/Asset picker %&#A", false, 5000)]
public static void ShowAssetPicker()
[MenuItem("Tools/Asset Picker %&#O", false, 5000)]
public static void OpenWindow()
{
var w = GetWindow<AssetPicker>();
w.focusNameFilter = true;
w.UpdateAssetList();
}

public static void ShowAssetPicker(
// todo: prettifty Show signatures
public static void Show(
[NotNull] Type assetType,
[CanBeNull] FieldInfo fieldInfo,
[NotNull] Action<Object> callback,
Expand Down Expand Up @@ -204,7 +205,11 @@ public static void ShowAssetPicker(
w.Focus();
}

public static void ShowAssetPicker<T>(Action<Object> callback, string[] labels, bool enableSelectionOnClick = false, Object selectedAsset = null)
public static void Show<T>(
[NotNull] Action<Object> callback,
[CanBeNull] string[] labels = null,
bool enableSelectionOnClick = false,
[CanBeNull] Object selectedAsset = null)
{
var w = CreateInstance<AssetPicker>();
w.enableSelectionOnClick = enableSelectionOnClick;
Expand All @@ -224,34 +229,20 @@ public static void ShowAssetPicker<T>(Action<Object> callback, string[] labels,
w.Focus();
}

public IEnumerable<Type> CollectAssetTypes()
{
yield return typeof(GameObject);
yield return typeof(Material);
yield return typeof(Texture);
yield return typeof(SceneAsset);
yield return typeof(ScriptableObject);

var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var gameAssembly = assemblies.Single(a => a.FullName.Contains("Assembly-CSharp,"));
var soTypes = TypeEx.GetSubclasses<ScriptableObject>(true, gameAssembly);

foreach (var type in soTypes)
{
yield return type;
}
}

// todo: c# docs

/// <summary>
/// Shows built-in ObjectField with custom value source and pick target
/// </summary>
public static void ShowPropertyField(
Rect position, SerializedProperty property, FieldInfo fieldInfo,
Object currentValue, Action<Object> pickCallback,
GUIContent label, Type assetType,
Func<HierarchyEntry, bool> filter = null)
public static void PropertyField(
Rect position,
[NotNull] SerializedProperty property,
[NotNull] FieldInfo fieldInfo,
[CanBeNull] Object currentValue,
[NotNull] Action<Object> pickCallback,
[NotNull] GUIContent label,
[NotNull] Type assetType,
[CanBeNull] Func<HierarchyEntry, bool> filter = null)
{
using (var scope = new EditorGUI.PropertyScope(position, label, property))
{
Expand Down Expand Up @@ -284,7 +275,7 @@ public static void ShowPropertyField(
else if (showHotKey || GUI.Button(buttonPos, "", GUIStyle.none))
{
// invisible button overrides object picker
ShowAssetPicker(
Show(
assetType,
fieldInfo,
o =>
Expand Down Expand Up @@ -315,7 +306,7 @@ public static void ShowPropertyField(

if (GUI.GetNameOfFocusedControl() == controlName)
{
// todo: fix copy paste
// todo: implement copy paste
// CopyPasteController.Process(assetType, property);
}

Expand All @@ -329,28 +320,37 @@ public static void ShowPropertyField(
/// <summary>
/// Shows built-in ObjectField, but overrides thumb button to call our AssetPicker window instead
/// </summary>
public static void ShowPropertyField(Rect position, SerializedProperty property, FieldInfo fieldInfo, GUIContent label, Type assetType, Func<HierarchyEntry, bool> filter = null)
public static void PropertyField(
Rect position,
[NotNull] SerializedProperty property,
[NotNull] FieldInfo fieldInfo,
[NotNull] GUIContent label,
[NotNull] Type assetType,
[CanBeNull] Func<HierarchyEntry, bool> filter = null)
{
Action<Object> pickCallback =
Action<Object> pickCallback =
o => property.objectReferenceValue = o;
ShowPropertyField(
PropertyField(
position, property, fieldInfo,
property.objectReferenceValue, pickCallback,
label, assetType, filter
);
}

public static void ShowObjectField(
Rect position, Object currentValue, Action<Object> pickCallback,
GUIContent label, Type assetType,
Func<HierarchyEntry, bool> filter = null)
public static void ObjectField(
Rect position,
[CanBeNull] Object currentValue,
[NotNull] Action<Object> pickCallback,
[NotNull] GUIContent label,
[NotNull] Type assetType,
[CanBeNull] Func<HierarchyEntry, bool> filter = null)
{
EditorGUI.BeginChangeCheck();
var buttonPos = position;
buttonPos.xMin = buttonPos.xMax - EditorGUIUtility.singleLineHeight;
var requesterWindow = focusedWindow;

string controlName = "ObjField"+label.text;
string controlName = "ObjField" + label.text;
var e = Event.current;
bool showHotKey =
GUI.GetNameOfFocusedControl() == controlName &&
Expand All @@ -374,7 +374,7 @@ public static void ShowObjectField(
else if (showHotKey || GUI.Button(buttonPos, "", GUIStyle.none))
{
// invisible button overrides object picker
ShowAssetPicker(
Show(
assetType,
null,
o =>
Expand Down Expand Up @@ -405,6 +405,24 @@ public static void ShowObjectField(
}
}

public IEnumerable<Type> CollectAssetTypes()
{
yield return typeof(GameObject);
yield return typeof(Material);
yield return typeof(Texture);
yield return typeof(SceneAsset);
yield return typeof(ScriptableObject);

var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var gameAssembly = assemblies.Single(a => a.FullName.Contains("Assembly-CSharp,"));
var soTypes = TypeEx.GetSubclasses<ScriptableObject>(true, gameAssembly);

foreach (var type in soTypes)
{
yield return type;
}
}

static AssetPicker()
{
}
Expand Down
5 changes: 4 additions & 1 deletion Assets/Editor/UnityPickers/AssetPickerDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
if (assetType.IsUnityCollection())
assetType = assetType.GetElementType();

if (assetType == null)
return;

var a = fieldInfo.GetAttribute<AssetPickerAttribute>();

AssetPicker.ShowPropertyField(
AssetPicker.PropertyField(
position, property, fieldInfo,
label, assetType,
he => a == null || he.Path.Contains(a.Path)
Expand Down
2 changes: 0 additions & 2 deletions Assets/Editor/UnityPickers/ValuePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public abstract class ValuePicker<T> : EditorWindow

private static GUIStyle selectedStyle;

// todo: previous selected style

[CanBeNull]
private T prevPicked;

Expand Down
3 changes: 3 additions & 0 deletions Assets/Example/Code/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ public class Monster : ScriptableObject

[AssetPicker("Example/Monsters/Prefabs")]
public MonsterView Prefab;

[AssetPicker]
public Texture Icon;
}
}
10 changes: 0 additions & 10 deletions Assets/Example/Code/Root.cs

This file was deleted.

24 changes: 24 additions & 0 deletions Assets/Example/Code/StartHereAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using UnityEngine;
using UnityPickers;

namespace Example.Code
{
[CreateAssetMenu(menuName = "Resources/StartHere")]
public class StartHereAsset : ScriptableObject
{
public MonsterType MonsterType;

public Monster Monster;

public Faction Faction;

[AssetPicker]
public GameObject AnyPrefab;

[AssetPicker("Example/Monsters/Prefabs")]
public MonsterView MonsterPrefab;

[AssetPicker]
public Texture Icon;
}
}
File renamed without changes.
13 changes: 7 additions & 6 deletions Assets/Example/Root.asset → Assets/Example/START_HERE.asset
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ MonoBehaviour:
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b39f6e84c4284def828908dace078c2c, type: 3}
m_Name: Root
m_Name: START_HERE
m_EditorClassIdentifier:
Monsters:
- {fileID: 11400000, guid: 8f68f7791183abd458bf9ebee40de012, type: 2}
- {fileID: 11400000, guid: 6236317bb59716342ba83c17cf36f989, type: 2}
- {fileID: 11400000, guid: 46f2f905e3d786a4f8ba428d82d846db, type: 2}
- {fileID: 11400000, guid: 4c150a27badb11c4ea87c7e99e154d3e, type: 2}
Monster: {fileID: 0}
Faction: {fileID: 0}
MonsterType: 0
AnyPrefab: {fileID: 0}
MonsterPrefab: {fileID: 0}
Icon: {fileID: 0}
File renamed without changes.
9 changes: 9 additions & 0 deletions Assets/Example/Textures.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Example/Textures/Circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ffb3387

Please sign in to comment.