public void AssetReferenceNoAsset_CreatesCorrectLabelForType() { // Base Types string expected = "None (Addressable Asset)"; string val = AssetReferenceDrawerUtilities.ConstructNoAssetLabel(typeof(AssetReference)); Assert.AreEqual(expected, val, "General Asset string expected for an unrestricted AssetReference"); val = AssetReferenceDrawerUtilities.ConstructNoAssetLabel(typeof(AssetReference[])); Assert.AreEqual(expected, val, "General Asset string expected for an unrestricted AssetReference"); val = AssetReferenceDrawerUtilities.ConstructNoAssetLabel(typeof(List <AssetReference>)); Assert.AreEqual(expected, val, "General Asset string expected for an unrestricted AssetReference"); expected = "None (Addressable GameObject)"; // generic types val = AssetReferenceDrawerUtilities.ConstructNoAssetLabel(typeof(AssetReferenceT <GameObject>)); Assert.AreEqual(expected, val, "Type restricted is expected in display string shown"); val = AssetReferenceDrawerUtilities.ConstructNoAssetLabel(typeof(AssetReferenceT <GameObject>[])); Assert.AreEqual(expected, val, "Type restricted is expected in display string shown"); val = AssetReferenceDrawerUtilities.ConstructNoAssetLabel(typeof(List <AssetReferenceT <GameObject> >)); Assert.AreEqual(expected, val, "Type restricted is expected in display string shown"); // inherited types val = AssetReferenceDrawerUtilities.ConstructNoAssetLabel(typeof(AssetReferenceGameObject)); Assert.AreEqual(expected, val, "Type restricted is expected in display string shown"); val = AssetReferenceDrawerUtilities.ConstructNoAssetLabel(typeof(AssetReferenceGameObject[])); Assert.AreEqual(expected, val, "Type restricted is expected in display string shown"); val = AssetReferenceDrawerUtilities.ConstructNoAssetLabel(typeof(List <AssetReferenceGameObject>)); Assert.AreEqual(expected, val, "Type restricted is expected in display string shown"); }
private void HandleDragAndDrop(SerializedProperty property, bool isDragging, bool isDropping, string guid) { var aaSettings = AddressableAssetSettingsDefaultObject.Settings; //During the drag, doing a light check on asset validity. The in-depth check happens during a drop, and should include a log if it fails. var rejectedDrag = false; if (isDragging) { if (aaSettings == null) { rejectedDrag = true; } else { var aaEntries = DragAndDrop.GetGenericData("AssetEntryTreeViewItem") as List <AssetEntryTreeViewItem>; rejectedDrag = AssetReferenceDrawerUtilities.ValidateDrag(m_AssetRefObject, Restrictions, aaEntries, DragAndDrop.objectReferences, DragAndDrop.paths); } DragAndDrop.visualMode = rejectedDrag ? DragAndDropVisualMode.Rejected : DragAndDropVisualMode.Copy; } if (!rejectedDrag && isDropping) { var aaEntries = DragAndDrop.GetGenericData("AssetEntryTreeViewItem") as List <AssetEntryTreeViewItem>; if (aaEntries != null) { if (aaEntries.Count == 1) { var item = aaEntries[0]; if (item.entry != null) { if (item.entry.IsInResources) { Addressables.LogWarning("Cannot use an AssetReference on an asset in Resources. Move asset out of Resources first."); } else { if (AssetReferenceDrawerUtilities.SetObject(ref m_AssetRefObject, ref m_ReferencesSame, property, item.entry.TargetAsset, fieldInfo, m_label.text, out guid)) { TriggerOnValidate(property); } } } } } else { if (DragAndDrop.paths != null && DragAndDrop.paths.Length == 1) { var path = DragAndDrop.paths[0]; DragAndDropNotFromAddressableGroupWindow(path, guid, property, aaSettings); } } } }
internal void GetSelectedSubassetIndex(List <Object> subAssets, out int selIndex, out string[] objNames) { var subAssetNames = subAssets.Select(sa => sa == null ? "<none>" : $"{AssetReferenceDrawerUtilities.FormatName(sa.name)}:{sa.GetType()}").ToList(); objNames = subAssetNames.ToArray(); selIndex = subAssetNames.IndexOf($"{m_AssetRefObject.SubObjectName}:{m_AssetRefObject.SubOjbectType}"); if (selIndex == -1) { selIndex = 0; } }
private Rect DrawSubAssetsControl(SerializedProperty property, List <Object> subAssets) { assetDropDownRect = new Rect(assetDropDownRect.position, new Vector2(assetDropDownRect.width / 2, assetDropDownRect.height)); var objRect = new Rect(assetDropDownRect.xMax, assetDropDownRect.y, assetDropDownRect.width, assetDropDownRect.height); float pickerWidth = 20f; Rect pickerRect = objRect; pickerRect.width = pickerWidth; pickerRect.x = objRect.xMax - pickerWidth; bool multipleSubassets = false; // Check if targetObjects have multiple different selected if (property.serializedObject.targetObjects.Length > 1) { multipleSubassets = AssetReferenceDrawerUtilities.CheckTargetObjectsSubassetsAreDifferent(property, m_AssetRefObject.SubObjectName, fieldInfo, m_label.text); } bool isPickerPressed = Event.current.type == EventType.MouseDown && Event.current.button == 0 && pickerRect.Contains(Event.current.mousePosition); if (isPickerPressed) { // Do custom popup with scroll to pick subasset if (m_SubassetPopup == null || m_SubassetPopup.m_property != property) { m_SubassetPopup = CreateSubAssetPopup(property, subAssets ?? AssetReferenceDrawerUtilities.GetSubAssetsList(m_AssetRefObject)); } PopupWindow.Show(objRect, m_SubassetPopup); } if (m_SubassetPopup != null && m_SubassetPopup.SelectionChanged) { m_SubassetPopup.UpdateSubAssets(); } // Show selected name GUIContent nameSelected = new GUIContent("--"); if (!multipleSubassets) { nameSelected.text = AssetReferenceDrawerUtilities.FormatName(m_AssetRefObject.SubObjectName); } UnityEngine.GUI.Box(objRect, nameSelected, EditorStyles.objectField); #if UNITY_2019_1_OR_NEWER // Draw picker arrow DrawCaret(pickerRect); #endif return(assetDropDownRect); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (property == null || label == null) { Debug.LogError("Error rendering drawer for AssetReference property."); return; } string labelText = label.text; m_ReferencesSame = true; m_AssetRefObject = property.GetActualObjectForSerializedProperty <AssetReference>(fieldInfo, ref labelText); labelText = ObjectNames.NicifyVariableName(labelText); if (labelText != label.text || string.IsNullOrEmpty(label.text)) { label = new GUIContent(labelText, label.tooltip); } m_label = label; if (m_AssetRefObject == null) { return; } EditorGUI.BeginProperty(position, label, property); if (m_Restrictions == null) { m_Restrictions = AssetReferenceDrawerUtilities.GatherFilters(property); } string guid = m_AssetRefObject.AssetGUID; var aaSettings = AddressableAssetSettingsDefaultObject.Settings; var isNotAddressable = ApplySelectionChanges(property, aaSettings, ref guid); assetDropDownRect = EditorGUI.PrefixLabel(position, label); var nameToUse = AssetReferenceDrawerUtilities.GetNameForAsset(ref m_ReferencesSame, property, isNotAddressable, fieldInfo, m_label.text); DrawSubassets(property); bool isDragging = Event.current.type == EventType.DragUpdated && position.Contains(Event.current.mousePosition); bool isDropping = Event.current.type == EventType.DragPerform && position.Contains(Event.current.mousePosition); DrawControl(property, isDragging, isDropping, nameToUse, isNotAddressable, guid); HandleDragAndDrop(property, isDragging, isDropping, guid); EditorGUI.EndProperty(); }
public void UpdateSubAssets() { if (selectionChanged) { if (!AssetReferenceDrawerUtilities.SetSubAssets(m_property, m_subAssets[SelectedIndex], m_drawer.fieldInfo, m_drawer.m_label.text)) { Debug.LogError("Unable to set all of the objects selected subassets"); } else { m_property.serializedObject.ApplyModifiedProperties(); m_property.serializedObject.Update(); m_property.FindPropertyRelative("m_EditorAssetChanged").boolValue = false; } selectionChanged = false; } }
private void DrawSubassets(SerializedProperty property) { if (m_AssetRefObject.editorAsset != null && m_ReferencesSame) { List <Object> subAssets = null; bool hasSubAssets = !string.IsNullOrEmpty(m_AssetRefObject.SubObjectName); if (!hasSubAssets) { subAssets = AssetReferenceDrawerUtilities.GetSubAssetsList(m_AssetRefObject); hasSubAssets = subAssets.Count > 1; } if (hasSubAssets) { assetDropDownRect = DrawSubAssetsControl(property, subAssets); } } }
bool ApplySelectionChanges(SerializedProperty property, AddressableAssetSettings aaSettings, ref string guid) { var checkToForceAddressable = string.Empty; if (!string.IsNullOrEmpty(newGuid) && newGuidPropertyPath == property.propertyPath) { if (newGuid == noAssetString) { if (AssetReferenceDrawerUtilities.SetObject(ref m_AssetRefObject, ref m_ReferencesSame, property, null, fieldInfo, m_label.text, out guid)) { TriggerOnValidate(property); } newGuid = string.Empty; } else if (newGuid == forceAddressableString) { checkToForceAddressable = guid; newGuid = string.Empty; } else if (guid != newGuid) { if (AssetReferenceDrawerUtilities.SetObject(ref m_AssetRefObject, ref m_ReferencesSame, property, AssetDatabase.LoadAssetAtPath <Object>(AssetDatabase.GUIDToAssetPath(newGuid)), fieldInfo, m_label.text, out guid)) { checkToForceAddressable = newGuid; TriggerOnValidate(property); } newGuid = string.Empty; } } bool isNotAddressable = false; m_AssetName = noAssetString; if (aaSettings != null && !string.IsNullOrEmpty(guid)) { isNotAddressable = AssetReferenceDrawerUtilities.CheckForNewEntry(ref m_AssetName, aaSettings, guid, checkToForceAddressable); } return(isNotAddressable); }
internal void DragAndDropNotFromAddressableGroupWindow(string path, string guid, SerializedProperty property, AddressableAssetSettings aaSettings) { if (AddressableAssetUtility.IsInResources(path)) { Addressables.LogWarning("Cannot use an AssetReference on an asset in Resources. Move asset out of Resources first. "); } else if (!AddressableAssetUtility.IsPathValidForEntry(path)) { Addressables.LogWarning("Dragged asset is not valid as an Asset Reference. " + path); } else { Object obj; if (DragAndDrop.objectReferences != null && DragAndDrop.objectReferences.Length == 1) { obj = DragAndDrop.objectReferences[0]; } else { obj = AssetDatabase.LoadAssetAtPath <Object>(path); } if (AssetReferenceDrawerUtilities.SetObject(ref m_AssetRefObject, ref m_ReferencesSame, property, obj, fieldInfo, m_label.text, out guid)) { TriggerOnValidate(property); aaSettings = AddressableAssetSettingsDefaultObject.GetSettings(true); var entry = aaSettings.FindAssetEntry(guid); if (entry == null && !string.IsNullOrEmpty(guid)) { string assetName; if (!aaSettings.IsAssetPathInAddressableDirectory(path, out assetName)) { aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup); newGuid = guid; } } } } }
internal bool ValidateAsset(IReferenceEntryData entryData) { return(AssetReferenceDrawerUtilities.ValidateAsset(m_AssetRefObject, Restrictions, entryData)); }
/// <summary> /// Validates that the referenced asset allowable for this asset reference. /// </summary> /// <param name="path">The path to the asset in question.</param> /// <returns>Whether the referenced asset is valid.</returns> public bool ValidateAsset(string path) { return(AssetReferenceDrawerUtilities.ValidateAsset(m_AssetRefObject, Restrictions, path)); }