private void UpdateSelectableComponents(Object dataContext) { if (componentPopup != null) { componentPopup.RemoveFromHierarchy(); componentPopup = null; } var targetGameObject = dataContext as GameObject; if (targetGameObject == null) { return; } var components = new List <Component>(); targetGameObject.GetComponents(components); var selectableObjects = components.Cast <Object>().ToList(); selectableObjects.Insert(0, dataContext); int defaultIndex = selectableObjects.IndexOf(pComponent.objectReferenceValue); if (defaultIndex < 0) { defaultIndex = 0; } //Make sure we're currently set to the default value if (selectableObjects.Count > 0) { pComponent.objectReferenceValue = selectableObjects[defaultIndex]; pComponent.serializedObject.ApplyModifiedProperties(); } componentPopup = new PopupField <Object>("Component", selectableObjects, defaultIndex, FormatComponentType, FormatComponentTypePopup); componentPopup.RegisterCallback <ChangeEvent <Object> >(evt => { pComponent.objectReferenceValue = evt.newValue is GameObject ? null : evt.newValue; pProperty.stringValue = string.Empty; UpdateSelectableProperties(); pComponent.serializedObject.ApplyModifiedProperties(); }); rootContainer.Insert(2, componentPopup); }
private void UpdateBonePopup(string[] names) { if (m_BonePopup != null) { m_BonePopup.RemoveFromHierarchy(); } m_BonePopup = new PopupField <string>(new List <string>(names), 0); m_BonePopup.name = "BonePopupField"; m_BonePopup.OnValueChanged((evt) => { bonePopupChanged(boneIndex); }); m_BonePopupContainer.Add(m_BonePopup); }
private void UpdateSelectableProperties() { if (propertyPopup != null) { propertyPopup.RemoveFromHierarchy(); propertyPopup = null; } Object targetObject = pComponent.objectReferenceValue; if (targetObject == null) { targetObject = pDataContext.objectReferenceValue; } if (targetObject == null) { return; } var properties = targetObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.GetField); var propertyNames = properties.Select(x => x.Name).ToList(); int defaultIndex = propertyNames.IndexOf(pProperty.stringValue); if (defaultIndex < 0) { defaultIndex = 0; } //Make sure we're currently set to the default value if (propertyNames.Count > 0) { pProperty.stringValue = propertyNames[defaultIndex]; pProperty.serializedObject.ApplyModifiedProperties(); } propertyPopup = new PopupField <string>("Property", propertyNames, defaultIndex); propertyPopup.RegisterCallback <ChangeEvent <string> >(evt => { pProperty.stringValue = evt.newValue; pProperty.serializedObject.ApplyModifiedProperties(); }); rootContainer.Add(propertyPopup); }
private void OnMaterialsDropped(Material[] droppedMaterials) { remappingRoot.Clear(); DisposeOfSerializedObjects(); //Construct a dictionary between shaders and their materials shadersToMaterials = new Dictionary <Shader, List <SerializedObject> >(); foreach (Material material in droppedMaterials) { if (!shadersToMaterials.TryGetValue(material.shader, out var list)) { list = new List <SerializedObject>(); shadersToMaterials.Add(material.shader, list); } list.Add(new SerializedObject(material)); } foreach (var pair in shadersToMaterials) { Box shaderRoot = new Box(); shaderRoot.AddToClassList("innerPadding"); remappingRoot.Add(shaderRoot); Shader shader = pair.Key; ObjectField shaderField = new ObjectField { objectType = typeof(Shader), value = shader }; shaderField.SetEnabled(false); shaderRoot.Add(shaderField); //RemapType, to a set of keys referring to values that could be remapped. var remapsFrom = new Dictionary <RemapType, HashSet <string> >(); var remapsTo = new Dictionary <RemapType, HashSet <string> >(); List <SerializedObject> materials = pair.Value; foreach (SerializedObject materialSO in materials) { SerializedProperty savedProperties = materialSO.FindProperty("m_SavedProperties"); //Textures ------------------------------------------------------------------------- SerializedProperty texEnvsArray = savedProperties.FindPropertyRelative("m_TexEnvs"); var textureKeys = new HashSet <string>(); remapsTo.Add(RemapType.Texture, textureKeys); var textureKeysFrom = new HashSet <string>(); remapsFrom.Add(RemapType.Texture, textureKeysFrom); for (int i = 0; i < texEnvsArray.arraySize; i++) { SerializedProperty texEnv = texEnvsArray.GetArrayElementAtIndex(i); SerializedProperty key = texEnv.FindPropertyRelative("first"); SerializedProperty texture = texEnv.FindPropertyRelative("second.m_Texture"); textureKeys.Add(key.stringValue); //Only add to the "from", if there is a key with a meaningful value in it. if (texture.objectReferenceValue != null) { textureKeysFrom.Add(key.stringValue); } } //----------------------------------------------------------------------------------- //Floats ---------------------------------------------------------------------------- SerializedProperty floatsArray = savedProperties.FindPropertyRelative("m_Floats"); //floats have no way of telling if they're not set, so they can use the same set var floatKeys = new HashSet <string>(); remapsTo.Add(RemapType.Float, floatKeys); remapsFrom.Add(RemapType.Float, floatKeys); for (int i = 0; i < floatsArray.arraySize; i++) { SerializedProperty @float = floatsArray.GetArrayElementAtIndex(i); SerializedProperty key = @float.FindPropertyRelative("first"); floatKeys.Add(key.stringValue); } //----------------------------------------------------------------------------------- //Vectors and Colors ---------------------------------------------------------------- SerializedProperty colorsArray = savedProperties.FindPropertyRelative("m_Colors"); //colors have no way of telling if they're not set, so they can use the same set var vectorKeys = new HashSet <string>(); remapsTo.Add(RemapType.VectorOrColor, vectorKeys); remapsFrom.Add(RemapType.VectorOrColor, vectorKeys); for (int i = 0; i < colorsArray.arraySize; i++) { SerializedProperty vector = colorsArray.GetArrayElementAtIndex(i); SerializedProperty key = vector.FindPropertyRelative("first"); vectorKeys.Add(key.stringValue); } //----------------------------------------------------------------------------------- } //CONTROLS var remapTypeField = new EnumField("Remap Type", RemapType.None) { name = remapTypeName }; remapTypeField.RegisterCallback <ChangeEvent <Enum> >(SelectedRemapType); shaderRoot.Add(remapTypeField); PopupField <string> popupFromField = new PopupField <string>(fromKeyLabel, noneList, 0) { name = fromKeyPopupName }; popupFromField.SetEnabled(false); shaderRoot.Add(popupFromField); PopupField <string> popupToField = new PopupField <string>(toKeyLabel, noneList, 0) { name = toKeyPopupName }; popupToField.SetEnabled(false); shaderRoot.Add(popupToField); Button remapButton = new Button { text = "Remap", name = remapButtonName }; remapButton.SetEnabled(false); shaderRoot.Add(remapButton); void SelectedRemapType(ChangeEvent <Enum> remapEvt) { if (remapEvt == null) { throw new ArgumentNullException(nameof(remapEvt)); } RemapType value = (RemapType)remapEvt.newValue; if (value == RemapType.None) { GetButton().SetEnabled(false); GetRemapTo().SetEnabled(false); GetRemapFrom().SetEnabled(false); return; } PopupField <string> child = GetRemapFrom(); int index = shaderRoot.IndexOf(child); child.RemoveFromHierarchy(); List <string> choicesFrom = remapsFrom[value].ToList(); choicesFrom.Insert(0, none); popupFromField = new PopupField <string>(fromKeyLabel, choicesFrom, none) { name = fromKeyPopupName }; shaderRoot.Insert(index, popupFromField); popupFromField.RegisterCallback <ChangeEvent <string> >(SelectedRemapFrom); } void SelectedRemapFrom(ChangeEvent <string> popupFromEvt) { string remapFromKey = popupFromEvt.newValue; if (remapFromKey == none) { GetButton().SetEnabled(false); GetRemapTo().SetEnabled(false); return; } PopupField <string> child = GetRemapTo(); int index = shaderRoot.IndexOf(child); child.RemoveFromHierarchy(); List <string> choicesTo = remapsTo[(RemapType)remapTypeField.value].ToList(); choicesTo.Insert(0, none); popupToField = new PopupField <string>(toKeyLabel, choicesTo, none) { name = toKeyPopupName }; shaderRoot.Insert(index, popupToField); popupToField.RegisterCallback <ChangeEvent <string>, (PopupField <string>, string)>(SelectedRemapTo, (popupToField, remapFromKey)); } void SelectedRemapTo(ChangeEvent <string> popupToEvt, (PopupField <string> popupToField, string remapFromKey) args) { string remapToKey = popupToEvt.newValue; VisualElement background = args.popupToField.Q(null, PopupField <string> .inputUssClassName); if (remapToKey == none) { GetButton().SetEnabled(false); background.style.unityBackgroundImageTintColor = default; return; } if (remapToKey == args.remapFromKey) { background.style.unityBackgroundImageTintColor = new Color(1f, 0.46f, 0.51f); return; } background.style.unityBackgroundImageTintColor = default; var button = GetButton(); button.SetEnabled(true); button.RemoveManipulator(button.clickable); button.clickable = new Clickable(() => PerformRemap(shader, (RemapType)GetRemapType().value, args.remapFromKey, remapToKey)); button.AddManipulator(button.clickable); } Button GetButton() => shaderRoot.Q <Button>(remapButtonName); EnumField GetRemapType() => shaderRoot.Q <EnumField>(remapTypeName); PopupField <string> GetRemapFrom() => shaderRoot.Q <PopupField <string> >(fromKeyPopupName); PopupField <string> GetRemapTo() => shaderRoot.Q <PopupField <string> >(toKeyPopupName); } }