/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// CreateNewDecal /// # Create a new mesh decal in the scene /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// public GenericMeshDecal CreateNewMeshDecal(Material decalMaterial, Transform parent, Vector3 point, Vector3 normal, float scaleMultiplier, Vector2 rotationRange, bool attachDecalToCollisionObject) { //print ("CreateNewDecal"); GenericMeshDecal actualDecal = Instantiate(decalPrefab.gameObject).GetComponent <GenericMeshDecal> (); actualDecal.material = decalMaterial; actualDecal.transform.position = point + 0.001f * normal; actualDecal.transform.localScale = scaleMultiplier * actualDecal.transform.localScale; actualDecal.transform.rotation = Quaternion.FromToRotation(Vector3.up, normal); if (attachDecalToCollisionObject) { actualDecal.transform.parent = parent; } else { GameObject decalsContainer = BasicFunctions.CreateContainerIfNotExists(BasicDefines.MESH_DECAL_CONTAINER_NAME); actualDecal.transform.parent = decalsContainer.transform; } actualDecal.rotationRange = rotationRange; actualDecal.name = "RunTimeDecal"; actualDecal.UpdateDecallShape(true, false); return(actualDecal.GetComponent <GenericMeshDecal> ()); }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// OnInspectorGUI /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// public override void OnInspectorGUI() { GenericMeshDecal decal = (GenericMeshDecal)target; bool updateShape = false; if (decal.material) { string actualMaterialName = decal.material.name; decal.material = EditorBasicFunctions.DrawMeshDecalElements(decal, false, new Rect(0, 0, EditorGUIUtility.currentViewWidth, EditorGUIUtility.singleLineHeight), null); if (decal.material.name != actualMaterialName) { updateShape = true; //Debug.Log ("Material Change: "+decal.material); decal.GetComponent <Renderer>().sharedMaterial = decal.material; } } if (GUI.changed || updateShape) { decal.UpdateDecallShape(false, false); } }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// HandleAllElementsEdition /// # To edit objects in scene using Unity controls /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// void HandleAllElementsEdition () { if (Event.current.type == EventType.MouseDrag) { //Debug.Log ("HandleAllElementsEdition -> MouseDrag"); if (Selection.activeGameObject) { GenericMeshDecal actualDecal = Selection.activeGameObject.GetComponent ("GenericMeshDecal") as GenericMeshDecal; if (actualDecal) { actualDecal.UpdateDecallShape (false, false); } } } }
/////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// HandleMeshDecalsMode /// # To insert mesh decals in scene using mouse /// </summary> /////////////////////////////////////////////////////////////////////////////////////////////////////// void HandleMeshDecalsMode() { if ((GetEditorTimeDiff() > 0.1f) && EditorBasicFunctions.GetMouseButtonDown(0) && EditorBasicFunctions.GetInsertModeKeyPressed() && !GetDoingSomethingSpecial()) { previousEditorTime = EditorApplication.timeSinceStartup; //Debug.Log ("Event.current.mousePosition: " + Event.current.mousePosition); Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hit; //Debug.Log ("-----------------------------------------------------------"); if (Physics.Raycast(ray, out hit)) { if (lastMeshDecalsHitPoint.ToString() == hit.point.ToString()) { //Debug.Log ("NOTE: same point duplicate -> lastMeshDecalsHitPoint: " + lastMeshDecalsHitPoint); } else { //Debug.Log ("lastMeshDecalsHitPoint A: " + lastMeshDecalsHitPoint); lastMeshDecalsHitPoint = hit.point; //Debug.Log ("hit.point: " + hit.point); //Debug.Log ("lastMeshDecalsHitPoint B: " + lastMeshDecalsHitPoint); //Debug.Log ("New Decal"); //Debug.Log ("Hit position: " + hit.point); //Debug.Log ("Collider Name: " + hit.collider.name); bool setPlanarDecal = EditorBasicFunctions.planarMeshDecals; if (hit.collider.GetComponent <Terrain> ()) { //Debug.Log ("It's a terrain, set the decal as planar"); setPlanarDecal = true; } meshDecalPrefab.planarDecal = setPlanarDecal; meshDecalPrefab.comeFromEditor = setPlanarDecal; GenericMeshDecal actualDecal = Instantiate(meshDecalPrefab.gameObject).GetComponent <GenericMeshDecal> (); actualDecal.transform.position = hit.point; meshDecalPrefab.comeFromEditor = false; Vector3 finalScale = Random.Range(actualDecal.scaleRange.x, actualDecal.scaleRange.y) * actualDecal.transform.localScale; float textureAspectRatio = (float)actualDecal.material.mainTexture.width / (float)actualDecal.material.mainTexture.height; if (actualDecal.GetPlanar()) { finalScale.x = 0.5f * finalScale.x; finalScale.y = textureAspectRatio * finalScale.x; } else { finalScale.x = textureAspectRatio * finalScale.x; } //finalScale.y = finalScale.x; //finalScale.z = finalScale.x; actualDecal.transform.localScale = finalScale; actualDecal.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); actualDecal.name = actualDecal.Generate(BasicDefines.MESH_DECAL_BASE_NAME, GetSeedForInstancies(), true, actualDecal.material.name); actualDecal.UpdateDecallShape(true, false); //Debug.Log ("actualDecal.attachToCollisionObject: " + actualDecal.attachToCollisionObject); if (actualDecal.attachToCollisionObject) { //Debug.Log ("Parent name: " + hit.collider.name); actualDecal.transform.parent = hit.collider.transform; } else { GameObject decalsContainer = BasicFunctions.CreateContainerIfNotExists(BasicDefines.MESH_DECAL_CONTAINER_NAME); actualDecal.transform.parent = decalsContainer.transform; } if (actualDecal.addCollider) { actualDecal.gameObject.AddComponent <MeshCollider> (); //actualDecal.gameObject.GetComponent<MeshCollider> ().convex = true; } actualObjectToForceSelect = actualDecal.gameObject; } } } }