[EDITOR] Is it possible to add gameobjects to selected gameobject in scene based on data (Skirting / Cornice / Molding to Walls etc)

Hi there,

I’m wondering if anyone can point me in the right direction to create an editor tool that would add a mesh to a GameObject’s children at selected points based on pivot location.


The usage:


For the project that I am making I
would like to add skirting boards and
cornice (The bits at the top and
bottom of the wall) to selected walls
in an attempt to stop myself from
having to unwrap each wall model
itself and just have a universal wall
that I can apply different materials
to. I have a scriptable object created
for data which includes the necessary
game object references and origin on
the Y axis.


The end goal:


to be able to scroll through different
styles on the fly and have that apply
to all selected walls.


Not looking for a direct answer although if you have one that’s awesome! But I’d love to learn about methods of achieving this. I’m new to editor scripts.

Thanks :slight_smile:

Several different ways to use the editor for this. One way is to just add a game object in the scene with this script attached:

using UnityEngine;
using System.Collections;

public class addmesh : MonoBehaviour 
{
	public string meshObjectNameToAddMeshToChildren;

 	public void RunAddMeshToChildren() {
		Transform t = GameObject.Find (meshObjectNameToAddMeshToChildren).transform;
		Transform [] transformsOfChildrenToAddMeshTo = t.GetComponentsInChildren<Transform>();
		for (int i = 1; i < transformsOfChildrenToAddMeshTo.Length; i++) {
			Debug.Log (transformsOfChildrenToAddMeshTo *.name);*
  •  	//you would want to add your mesh logic here on the children or whaterver other functionality you want it to do*
    
  •  }*
    
  • }*
    }
    And your editor script in a different script would be:
    using UnityEngine;
    using UnityEditor;

#if UNITY_EDITOR
[CustomEditor(typeof(addmesh))]

public class editoraddmesh : Editor
{

  • public override void OnInspectorGUI()*

  • {*

  •  DrawDefaultInspector();*
    
  •  addmesh myScript = (addmesh)target;*
    
  •  if(GUILayout.Button("Add Mesh To Children"))*
    
  •  {*
    
  •  	myScript.RunAddMeshToChildren();*
    
  •  }*
    
  • }*
    }
    #endif
    You would use this by adding the name of the object that you want to cycle through the children with the meshObjectNameToAddMeshToChildren variable. And just click the Add Mesh To Children for it to work.
    Setup would look something like this:
    ![alt text][1]
    [1]: https://www.dropbox.com/s/dt1bp4fbji9afnv/Screen%20Shot%202018-02-04%20at%2010.59.40%20PM.png?raw=1