public void OnPreprocessModel()
        {
            _colors = new Dictionary <int, List <Color> >();

            _importMaterialColours = false;

            var customAssetData = CustomAssetData.Get(assetImporter);

            if (customAssetData == null)
            {
                _shouldProcess = _importMaterialColours = ModelColourEditorSettings.Instance.importMaterialColoursByDefault;
            }
            else
            {
                var hasMeshColours = customAssetData.meshColors != null && customAssetData.meshColors.Count > 0;
                _importMaterialColours = customAssetData.ShouldImportMaterialColors;
                _shouldProcess         = hasMeshColours || _importMaterialColours;
            }

            if (_shouldProcess)
            {
                var importer = assetImporter as ModelImporter;
                if (importer != null)
                {
                    importer.materialImportMode = ModelImporterMaterialImportMode.None;
                }
            }
        }
        public void OnPostprocessModel(GameObject root)
        {
            if (!_shouldProcess)
            {
                return;
            }

            HashSet <string> meshHashSet = null;

            if (!_importMaterialColours)
            {
                var customAssetData = CustomAssetData.Get(assetImporter);
                var meshColorData   = customAssetData.meshColors;
                meshHashSet = new HashSet <string>(meshColorData.Select(mc => mc.meshName));
            }

            foreach (var meshFilter in root.GetComponentsInChildren <MeshFilter>())
            {
                ProcessMesh(meshFilter.sharedMesh, meshFilter.GetComponent <Renderer>(), meshHashSet);
            }

            foreach (var skinnedMeshRenderer in root.GetComponentsInChildren <SkinnedMeshRenderer>())
            {
                ProcessMesh(skinnedMeshRenderer.sharedMesh, skinnedMeshRenderer, meshHashSet);
            }

            Debug.Log($"Imported material to vertex colours {assetPath}");
        }
Example #3
0
        private void Build()
        {
            var template = Resources.Load <VisualTreeAsset>(Resource.MODEL_COLOUR_EDITOR_IMPORT_MATERIALS_TEMPLATE);

            template.CloneTree(this);

            _label      = this.Q <Label>("importMaterialColoursName");
            _label.text = _asset.name;

            _enableButton  = this.Q <Button>("enableMaterialColoursButton");
            _disableButton = this.Q <Button>("disableMaterialColoursButton");
            _resetButton   = this.Q <Button>("resetMaterialColoursButton");

            _enableButton.clicked  += () => SetImportMaterialsEnabled(true);
            _disableButton.clicked += () => SetImportMaterialsEnabled(false);
            _resetButton.clicked   += () => SetImportMaterialsEnabled(null);

            string path      = AssetDatabase.GetAssetPath(_asset);
            var    data      = CustomAssetData.Get(_asset);
            bool   isEnabled = data?.ShouldImportMaterialColors ?? ModelColourEditorSettings.Instance.importMaterialColoursByDefault;
            bool   hasValue  = data?.importMaterialColors.hasValue ?? false;

            _enableButton.SetEnabled(!isEnabled);
            _disableButton.SetEnabled(isEnabled);
            _resetButton.SetEnabled(hasValue);
        }
Example #4
0
        public void OnPostprocessModel(GameObject root)
        {
            var customAssetData = CustomAssetData.Get(assetImporter);

            if (customAssetData == null)
            {
                return;
            }
            if (!customAssetData.HasMeshColours)
            {
                return;
            }

            var meshColorData = customAssetData.meshColors;
            IReadOnlyDictionary <string, IGrouping <string, CustomAssetData.MeshColor> > meshDictionary =
                meshColorData.GroupBy(mc => mc.meshName).ToDictionary(mc => mc.Key);

            foreach (var meshFilter in root.GetComponentsInChildren <MeshFilter>())
            {
                ProcessMesh(meshFilter.sharedMesh, meshFilter.GetComponent <MeshRenderer>(), meshDictionary);
            }

            foreach (var skinnedMeshRenderer in root.GetComponentsInChildren <SkinnedMeshRenderer>())
            {
                ProcessMesh(skinnedMeshRenderer.sharedMesh, skinnedMeshRenderer, meshDictionary);
            }
        }
Example #5
0
        public static void Set(UnityEngine.Object asset, CustomAssetData data)
        {
            var path     = AssetDatabase.GetAssetPath(asset);
            var importer = AssetImporter.GetAtPath(path);

            importer.userData = JsonUtility.ToJson(data);
            EditorUtility.SetDirty(asset);
        }
Example #6
0
        public static void Set(string path, CustomAssetData data)
        {
            var asset    = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(path);
            var importer = AssetImporter.GetAtPath(path);

            importer.userData = JsonUtility.ToJson(data);
            EditorUtility.SetDirty(asset);
        }
Example #7
0
        private void SetImportMaterialsEnabled(bool?enabled)
        {
            string          path     = AssetDatabase.GetAssetPath(_asset);
            var             importer = AssetImporter.GetAtPath(path);
            CustomAssetData data     = CustomAssetData.Get(_asset) ?? new CustomAssetData();

            data.importMaterialColors = enabled;

            CustomAssetData.Set(path, data);

            // if (enabled) { (importer as ModelImporter).materialImportMode = ModelImporterMaterialImportMode.None; }
            AssetDatabase.ImportAsset(path);

            OnImportMaterialsChangedEvent?.Invoke();
        }
        private void ApplyChangeToMeshes(string multipleWarning, MeshDataAction action)
        {
            // Dialogue popup warning

            var selectedIndices = _previewColorElement.GetSelectedIndices();

            var colourSlotsCount = selectedIndices.Count;

            if (colourSlotsCount > 1)
            {
                if (!EditorUtility.DisplayDialog("Multiple mesh confirmation", $"You have {colourSlotsCount} colour slots selected. {multipleWarning}", "Yes", "No"))
                {
                    return;
                }
            }

            var modelData  = new Dictionary <GameObject, CustomAssetData>();
            var meshColors = selectedIndices.Select(i => i >= _previewColorElement.ColourSlotCount ? _colourSlotsWithoutColours[i - _previewColorElement.ColourSlotCount] : _previewColours[i]); // Empty colour slots indexed from 0 after slot count exceeded

            foreach (var meshColor in meshColors)
            {
                var mesh  = meshColor.mesh;
                var index = meshColor.materialIndex;
                var asset = _meshModelDictionary[meshColor.mesh];
                if (!modelData.ContainsKey(asset))
                {
                    CustomAssetData data = CustomAssetData.Get(asset) ?? new CustomAssetData();
                    modelData.Add(asset, data);
                }

                modelData[asset] = action(mesh, index, modelData[asset]);
            }

            foreach (var data in modelData)
            {
                string path = AssetDatabase.GetAssetPath(data.Key);
                CustomAssetData.Set(data.Key, data.Value);
                AssetDatabase.ImportAsset(path);
            }

            _modelDataDictionary.Clear();

            OnSelectionChanged();
        }
        private void ApplyChangeToModels(string multipleWarning, ModelDataAction action)
        {
            // Dialogue popup warning
            if (_selectedModels.Count > 1)
            {
                if (!EditorUtility.DisplayDialog("Multiple asset confirmation", $"You have {_selectedModels.Count} assets selected. {multipleWarning}", "Yes", "No"))
                {
                    return;
                }
            }

            foreach (var model in _selectedModels)
            {
                string          path = AssetDatabase.GetAssetPath(model);
                CustomAssetData data = CustomAssetData.Get(model) ?? new CustomAssetData();
                data = action(model, data);
                CustomAssetData.Set(path, data);
                AssetDatabase.ImportAsset(path);
            }

            _modelDataDictionary.Clear();

            OnSelectionChanged();
        }
        private void GetSceneReferencesForMesh(Mesh mesh)
        {
            for (int i = _meshFilters.Count - 1; i >= 0; i--)
            {
                if (_meshFilters[i].sharedMesh == mesh)
                {
                    _sceneReferencesCount++;
                    _meshFilters.RemoveAt(i);
                }
            }

            for (int i = _skinnedMeshRenderers.Count - 1; i >= 0; i--)
            {
                if (_skinnedMeshRenderers[i].sharedMesh == mesh)
                {
                    _sceneReferencesCount++;
                    _skinnedMeshRenderers.RemoveAt(i);
                }
            }

            if (_hasMaterialImportColour && _hasEditorVertexColour)
            {
                return;
            }

            GameObject model = _meshModelDictionary[mesh];

            if (!_modelDataDictionary.TryGetValue(model, out var data))
            {
                data = CustomAssetData.Get(model);
                _modelDataDictionary.Add(model, data);
            }

            _hasEditorVertexColour   |= data?.meshColors.Any(mc => mc.meshName == mesh.name) ?? false;
            _hasMaterialImportColour |= data?.ShouldImportMaterialColors ?? false;
        }