Example #1
0
        void OnGUI()
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Attach Mode");
            AttachColliderEditor.Mode currentAttachMode = (AttachColliderEditor.Mode)EditorGUILayout.EnumPopup((AttachColliderEditor.Mode)PlayerPrefs.GetInt("AttachColliderEditor_Attach_Mode", (int)AttachColliderEditor.Mode.MaxVolumn));
            if (currentAttachMode != attachMode)
            {
                attachMode = currentAttachMode;
                PlayerPrefs.SetInt("AttachColliderEditor_Attach_Mode", (int)attachMode);
            }
            EditorGUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Filter MeshRenderer Type");
            filterMeshRendererTypes = (ThreedObjectControlEditor.FilterMeshRendererTypes)EditorGUILayout.EnumPopup((ThreedObjectControlEditor.FilterMeshRendererTypes)PlayerPrefs.GetInt("AttachColliderEditor_Filter_MeshRenderer_Type", (int)filterMeshRendererTypes));
            PlayerPrefs.SetInt("AttachColliderEditor_Filter_MeshRenderer_Type", (int)filterMeshRendererTypes);
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Attach Collider Type");
            attachColliderTypes = (ThreedObjectControlEditor.AttachColliderTypes)EditorGUILayout.EnumPopup((ThreedObjectControlEditor.AttachColliderTypes)PlayerPrefs.GetInt("AttachColliderEditor_Attach_Collider_Type", (int)attachColliderTypes));
            PlayerPrefs.SetInt("AttachColliderEditor_Attach_Collider_Type", (int)attachColliderTypes);
            GUILayout.EndHorizontal();

            // Unity EditorのUI
            GUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Search Root Directory");
            searchRootDirectory = (string)EditorGUILayout.TextField(PlayerPrefs.GetString("AttachColliderEditor_Search_Root_Directory", searchRootDirectory));
            PlayerPrefs.SetString("AttachColliderEditor_Search_Root_Directory", searchRootDirectory);
            GUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button(new GUIContent("Execute")))
            {
                if (string.IsNullOrEmpty(searchRootDirectory))
                {
                    return;
                }

                if (attachMode == AttachColliderEditor.Mode.MaxVolumn)
                {
                    ThreedObjectControlEditor.AttachColliderMaxVolumn(searchRootDirectory, filterMeshRendererTypes: filterMeshRendererTypes, attachColliderTypes: attachColliderTypes);
                }
                else if (attachMode == AttachColliderEditor.Mode.WiddestSize)
                {
                    ThreedObjectControlEditor.AttachColliderWiddestSize(searchRootDirectory, filterMeshRendererTypes: filterMeshRendererTypes, attachColliderTypes: attachColliderTypes);
                }
            }
            GUILayout.EndHorizontal();
        }
Example #2
0
        public static void AttachColliderWiddestSize(string searchRootDirectory,
                                                     ThreedObjectControlEditor.FilterMeshRendererTypes filterMeshRendererTypes = ThreedObjectControlEditor.FilterMeshRendererTypes.OnlySkinnedMeshRenderer,
                                                     ThreedObjectControlEditor.AttachColliderTypes attachColliderTypes         = ThreedObjectControlEditor.AttachColliderTypes.BoxCollider)
        {
            List <string> pathes = FindAllThreedSearchDirectory(searchRootDirectory, SearchThreedObjectFileExtention.prefab);
            Dictionary <GameObject, KeyValuePair <Vector3, Vector3> > objectMinMaxPositions = new Dictionary <GameObject, KeyValuePair <Vector3, Vector3> >();

            for (int i = 0; i < pathes.Count; ++i)
            {
                string     path = pathes[i];
                GameObject go   = AssetDatabase.LoadAssetAtPath <GameObject>(path);
                if (go == null)
                {
                    continue;
                }
                List <Renderer> renderers = LoadMeshRenderers(go, filterMeshRendererTypes);
                if (renderers == null || renderers.Count <= 0)
                {
                    continue;
                }
                Vector3 minimumPosition  = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
                Vector3 maximumPosition  = new Vector3(float.MinValue, float.MinValue, float.MinValue);
                bool    isAttachCollider = false;
                for (int j = 0; j < renderers.Count; ++j)
                {
                    isAttachCollider = true;

                    Bounds bounds = renderers[j].bounds;
                    if (minimumPosition.x > bounds.center.x - bounds.extents.x)
                    {
                        minimumPosition.x = bounds.center.x - bounds.extents.x;
                    }
                    if (minimumPosition.y > bounds.center.y - bounds.extents.y)
                    {
                        minimumPosition.y = bounds.center.y - bounds.extents.y;
                    }
                    if (minimumPosition.z > bounds.center.z - bounds.extents.z)
                    {
                        minimumPosition.z = bounds.center.z - bounds.extents.z;
                    }
                    if (maximumPosition.x < bounds.center.x + bounds.extents.x)
                    {
                        maximumPosition.x = bounds.center.x + bounds.extents.x;
                    }
                    if (maximumPosition.y < bounds.center.y + bounds.extents.y)
                    {
                        maximumPosition.y = bounds.center.y + bounds.extents.y;
                    }
                    if (maximumPosition.z < bounds.center.z + bounds.extents.z)
                    {
                        maximumPosition.z = bounds.center.z + bounds.extents.z;
                    }
                }
                if (isAttachCollider)
                {
                    objectMinMaxPositions.Add(go, new KeyValuePair <Vector3, Vector3>(minimumPosition, maximumPosition));
                }
            }

            AssetDatabase.StartAssetEditing();
            foreach (KeyValuePair <GameObject, KeyValuePair <Vector3, Vector3> > objectMinMaxPosition in objectMinMaxPositions)
            {
                GameObject go = objectMinMaxPosition.Key;
                KeyValuePair <Vector3, Vector3> minmaxPosition = objectMinMaxPosition.Value;
                if (attachColliderTypes == ThreedObjectControlEditor.AttachColliderTypes.BoxCollider)
                {
                    BoxCollider boxCollider = go.GetComponent <BoxCollider>();
                    if (boxCollider.Equals(null))
                    {
                        boxCollider = go.AddComponent <BoxCollider>();
                    }
                    boxCollider.center = (minmaxPosition.Value + minmaxPosition.Key) / 2;
                    boxCollider.size   = (minmaxPosition.Value - minmaxPosition.Key);
                }
                else if (attachColliderTypes == ThreedObjectControlEditor.AttachColliderTypes.CapsuleCollider)
                {
                    CapsuleCollider capsuleCollider = go.GetComponent <CapsuleCollider>();
                    if (capsuleCollider.Equals(null))
                    {
                        capsuleCollider = go.AddComponent <CapsuleCollider>();
                    }
                    capsuleCollider.center = (minmaxPosition.Value + minmaxPosition.Key) / 2;
                    capsuleCollider.height = Mathf.Abs(minmaxPosition.Value.y - minmaxPosition.Key.y);
                    Vector3 diff = minmaxPosition.Value - minmaxPosition.Key;
                    capsuleCollider.radius = Mathf.Max(Mathf.Max(Mathf.Abs(diff.x / 2), Mathf.Abs(diff.y / 2)), Mathf.Abs(diff.z / 2));
                }
                else if (attachColliderTypes == ThreedObjectControlEditor.AttachColliderTypes.SphereCollider)
                {
                    SphereCollider sphereCollider = go.GetComponent <SphereCollider>();
                    if (sphereCollider.Equals(null))
                    {
                        sphereCollider = go.AddComponent <SphereCollider>();
                    }
                    sphereCollider.center = (minmaxPosition.Value + minmaxPosition.Key) / 2;
                    Vector3 diff = minmaxPosition.Value - minmaxPosition.Key;
                    sphereCollider.radius = Mathf.Max(Mathf.Abs(diff.x / 2), Mathf.Abs(diff.z / 2));
                }
                else if (attachColliderTypes == ThreedObjectControlEditor.AttachColliderTypes.MeshCollider)
                {
                    MeshCollider meshCollider = go.GetComponent <MeshCollider>();
                    if (meshCollider.Equals(null))
                    {
                        meshCollider = go.AddComponent <MeshCollider>();
                    }
                }
            }
            AssetDatabase.StopAssetEditing();
            foreach (KeyValuePair <GameObject, KeyValuePair <Vector3, Vector3> > objectMinMaxPosition in objectMinMaxPositions)
            {
                EditorUtility.SetDirty(objectMinMaxPosition.Key);
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Debug.Log("Attach and edit the widdest colliders:" + objectMinMaxPositions.Count);
        }
Example #3
0
        public static void AttachColliderMaxVolumn(string searchRootDirectory,
                                                   ThreedObjectControlEditor.FilterMeshRendererTypes filterMeshRendererTypes = ThreedObjectControlEditor.FilterMeshRendererTypes.OnlySkinnedMeshRenderer,
                                                   ThreedObjectControlEditor.AttachColliderTypes attachColliderTypes         = ThreedObjectControlEditor.AttachColliderTypes.BoxCollider)
        {
            List <string> pathes = FindAllThreedSearchDirectory(searchRootDirectory, SearchThreedObjectFileExtention.prefab);
            Dictionary <GameObject, Renderer> objectRenderers = new Dictionary <GameObject, Renderer>();

            for (int i = 0; i < pathes.Count; ++i)
            {
                string     path = pathes[i];
                GameObject go   = AssetDatabase.LoadAssetAtPath <GameObject>(path);
                if (go == null)
                {
                    continue;
                }
                List <Renderer> renderers = LoadMeshRenderers(go, filterMeshRendererTypes);
                if (renderers == null || renderers.Count <= 0)
                {
                    continue;
                }
                float maxVolumn   = float.MinValue;
                int   targetIndex = -1;
                for (int j = 0; j < renderers.Count; ++j)
                {
                    Vector3 cubeSize = renderers[j].bounds.extents * 2;
                    if (maxVolumn < (cubeSize.x * cubeSize.y * cubeSize.z))
                    {
                        maxVolumn   = (cubeSize.x * cubeSize.y * cubeSize.z);
                        targetIndex = j;
                    }
                }
                if (targetIndex < 0)
                {
                    continue;
                }
                objectRenderers.Add(go, renderers[targetIndex]);
            }
            AssetDatabase.StartAssetEditing();
            foreach (KeyValuePair <GameObject, Renderer> objectRenderer in objectRenderers)
            {
                GameObject go           = objectRenderer.Key;
                Renderer   meshRenderer = objectRenderer.Value;
                Bounds     bounds       = meshRenderer.bounds;
                if (attachColliderTypes == ThreedObjectControlEditor.AttachColliderTypes.BoxCollider)
                {
                    BoxCollider boxCollider = go.GetComponent <BoxCollider>();
                    if (boxCollider.Equals(null))
                    {
                        boxCollider = go.AddComponent <BoxCollider>();
                    }
                    boxCollider.center = bounds.center;
                    boxCollider.size   = bounds.extents * 2;
                }
                else if (attachColliderTypes == ThreedObjectControlEditor.AttachColliderTypes.CapsuleCollider)
                {
                    CapsuleCollider capsuleCollider = go.GetComponent <CapsuleCollider>();
                    if (capsuleCollider.Equals(null))
                    {
                        capsuleCollider = go.AddComponent <CapsuleCollider>();
                    }
                    capsuleCollider.center = bounds.center;
                    capsuleCollider.height = Mathf.Abs(bounds.extents.y * 2);
                    capsuleCollider.radius = Mathf.Max(Mathf.Abs(bounds.extents.x), Mathf.Abs(bounds.extents.z));
                }
                else if (attachColliderTypes == ThreedObjectControlEditor.AttachColliderTypes.SphereCollider)
                {
                    SphereCollider sphereCollider = go.GetComponent <SphereCollider>();
                    if (sphereCollider.Equals(null))
                    {
                        sphereCollider = go.AddComponent <SphereCollider>();
                    }
                    sphereCollider.center = bounds.center;
                    sphereCollider.radius = Mathf.Max(Mathf.Max(Mathf.Abs(bounds.extents.x), Mathf.Abs(bounds.extents.y)), Mathf.Abs(bounds.extents.z));
                }
                else if (attachColliderTypes == ThreedObjectControlEditor.AttachColliderTypes.MeshCollider)
                {
                    MeshCollider meshCollider = go.GetComponent <MeshCollider>();
                    if (meshCollider.Equals(null))
                    {
                        meshCollider = go.AddComponent <MeshCollider>();
                    }
                }
            }

            AssetDatabase.StopAssetEditing();
            foreach (KeyValuePair <GameObject, Renderer> objectMeshRenderer in objectRenderers)
            {
                EditorUtility.SetDirty(objectMeshRenderer.Key);
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Debug.Log("Attach and edit the max volumn colliders:" + objectRenderers.Count);
        }