示例#1
0
        // Use this for initialization
        public void GenerateGrabObject()
        {
#if UNITY_EDITOR
            // Create a child
            GameObject gobj = new GameObject("GrabCollider");
            gobj.transform.parent        = transform;
            gobj.transform.localPosition = Vector3.zero;
            gobj.transform.localRotation = Quaternion.identity;
            gobj.transform.localScale    = Vector3.one;

            // Copy collider, add rigidbody
            Collider cd = GetComponent <Collider>();
            UnityEditorInternal.ComponentUtility.CopyComponent(cd);
            UnityEditorInternal.ComponentUtility.PasteComponentAsNew(gobj);
            Collider ncd = gobj.GetComponent <Collider>();
            ncd.isTrigger = true;

            Rigidbody nrb = gobj.AddComponent <Rigidbody>();
            nrb.useGravity = false;

            // Add Script
            GrabCollider grabc = gobj.AddComponent <GrabCollider>();
            grabc.SetBindObject(transform);

            grabc.newMaterial     = (Material)AssetDatabase.LoadAssetAtPath("Assets/Materials/OutlineEffect/OutlineEffect/VerticesOutline.mat", typeof(Material));
            grabc.AutomaticExpand = true;
#endif
        }
        public static void SetGrabableLegacy()
        {
            GameObject targetObj = Selection.activeGameObject;

            if (targetObj != null)
            {
                Collider cd = targetObj.GetComponent <Collider>();
                if (cd == null)
                {
                    UnityEditor.EditorUtility.DisplayDialog("Error", "The grabable object must have a collider" +
                                                            "component", "OK");
                    return;
                }

                // check ridigbody
                Rigidbody rd = targetObj.GetComponent <Rigidbody>();
                if (rd == null)
                {
                    rd            = targetObj.AddComponent <Rigidbody>();
                    rd.useGravity = false;
                }

                // Create child
                GameObject checkobj = ObjectFactory.CreateGameObject("GrabCollider");
                targetObj.layer                  = 11;
                checkobj.transform.parent        = targetObj.transform;
                checkobj.transform.localPosition = Vector3.zero;
                checkobj.transform.localRotation = Quaternion.identity;
                checkobj.transform.localScale    = Vector3.one;
                // Copy collider
                UnityEditorInternal.ComponentUtility.CopyComponent(cd);
                UnityEditorInternal.ComponentUtility.PasteComponentAsNew(checkobj);
                // Set trigger
                Collider ccd = checkobj.GetComponent <Collider>();
                ccd.isTrigger = true;
                // Add rigidbody
                Rigidbody crb = checkobj.AddComponent <Rigidbody>();
                crb.useGravity = false;
                // Add Script
                GrabCollider cgc = checkobj.AddComponent <GrabCollider>();
                cgc.AutomaticExpand = true;
                cgc.newMaterial     = AssetDatabase.LoadAssetAtPath(outlineMaterialPath,
                                                                    typeof(Material)) as Material;
                cgc.SetBindObject(targetObj.transform);

                // Set tag
                targetObj.tag = "InteractableObj";
            }
        }