Hi, I bought unity indie yesterday. It’s a lot nicer than the traditional means of update text file, run game, update text file, run game… Anyway, I’m converting a project over from a not-so-great engine (my own, lol). My old level layout was done in Blender, which I’d export.
With a little tweaking I can get the majority of the assets into unity, looking true to the originals, very nice. But I’ve got a slightly odd problem to do with trees. I have a forest map with a couple of hundred trees, and I render the shadow maps for the floor in Blender using those trees so it’s very important that their positions match up.
When I put the blend file in the assets, rather than instancing the one tree mesh it creates tree.001 tree.002 all the way up to 200 duplicate meshes, not what I’d call efficient.
So what I want to know is there a search and replace feature? Like I’d select a bunch of gameobjects, hit replace and then the chosen prefab would be created in place of each object. (The idea of manually positioning 200 trees makes me ill. XD)
Then make another script that loads the location and rotations and instantiates the trees at each location.
The trick is to keep the trees after you stop the game, and I did read a post where you could do it but can’t find it off hand. I think it was to select all the trees and Copy them while the game is running, then Paste them back after you stop the game.
I’ve gone for the editor script route 'cause it’s more useful. Only took an hour to figure out with those examples. I couldn’t figure out if there was a way to keep pref connection with scene objects but eh, at least I can instance the prefabs themselves fine. Also you can undo it (most expensive undo but this operation isn’t something to be done on a casual basis is it
Edit: Added keep originals and a rotation offset for those stubborn asset hierarchies.
/*
* This wizard will replace a selection with an object or prefab.
* Scene objects will be cloned (destroying their prefab links).
*/
using UnityEngine;
using UnityEditor;
using System.Collections;
public class ReplaceSelection : ScriptableWizard
{
static GameObject replacement = null;
static bool rotation = true;
static bool keep = false;
static Vector3 roffset = Vector3.zero;
public GameObject ReplacementObject = null;
public bool ApplyRotation = true;
public bool KeepOriginals = false;
public Vector3 OffsetRotation = Vector3.zero;
[MenuItem("GameObject/Replace Selection...")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard(
"Replace Selection", typeof(ReplaceSelection), "Replace");
}
public ReplaceSelection()
{
ReplacementObject = replacement;
ApplyRotation = rotation;
KeepOriginals = keep;
OffsetRotation = roffset;
}
void OnWizardUpdate()
{
replacement = ReplacementObject;
rotation = ApplyRotation;
keep = KeepOriginals;
roffset = OffsetRotation;
}
void OnWizardCreate()
{
if (replacement == null)
return;
Undo.RegisterSceneUndo("Replace Selection");
Transform[] transforms = Selection.GetTransforms(
SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
foreach (Transform t in transforms)
{
GameObject g;
PrefabType pref = EditorUtility.GetPrefabType(replacement);
if (pref == PrefabType.Prefab || pref == PrefabType.ModelPrefab)
{
g = (GameObject)EditorUtility.InstantiatePrefab(replacement);
}
else
{
g = (GameObject)Editor.Instantiate(replacement);
}
g.name = replacement.name;
g.transform.position = t.position;
if (rotation)
{
g.transform.rotation = t.rotation * Quaternion.Euler(roffset);
}
}
if (!keep)
{
foreach (GameObject g in Selection.gameObjects)
{
GameObject.DestroyImmediate(g);
}
}
}
}