PlaceSelectionOnSurface

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</csharp>" to "</syntaxhighlight>")
 
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
[[Category: Wizard]]
 +
[[Category: ScriptableObject]]
 +
[[Category: C Sharp]]
 +
[[Category: Raycast]]
 +
[[Category: Selection]]
 
Author: Jonathan Czeck (aarku)
 
Author: Jonathan Czeck (aarku)
-----
+
==Description==
 +
This wizard takes the current selection and raycasts downward from each object and moves them to where the raycast hit.  It is useful for placing a lot of items on top of a terrain.
  
 +
It is not bulletproof to say the least, but it provides a starting point.
 +
 +
==Usage==
 
Place this script in ''YourProject/Assets/Editor'' and a menu item will automatically appear in the GameObject menu after it is compiled.
 
Place this script in ''YourProject/Assets/Editor'' and a menu item will automatically appear in the GameObject menu after it is compiled.
  
This wizard takes the current selection and raycasts downward from each object and moves them to where they hit.  It is useful for placing a lot of items on top of a terrain.
+
==C# - PlaceSelectionOnSurface.cs==
 
+
<syntaxhighlight lang="csharp">
It is not bulletproof to say the least, but it provides a starting ground.
+
 
+
 
  using UnityEngine;
 
  using UnityEngine;
 
  using UnityEditor;
 
  using UnityEditor;
Line 13: Line 20:
 
  public class PlaceSelectionOnSurface : ScriptableObject
 
  public class PlaceSelectionOnSurface : ScriptableObject
 
  {
 
  {
[MenuItem ("GameObject/Place Selection On Surface")]
+
    [MenuItem ("GameObject/Place Selection On Surface")]
static void CreateWizard ()
+
    static void CreateWizard ()
{
+
    {
Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep | SelectionMode.ExcludePrefab | SelectionMode.OnlyUserModifyable);
+
        Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep |
if (transforms.Length > 0 && EditorUtility.DisplayDialog("Place Selection On Surface?", "Are you sure you want to place " + transforms.Length + ((transforms.Length > 1) ? " objects" : " object") + " on the surface in the -Y direction?", "Place", "Do Not Place"))
+
            SelectionMode.ExcludePrefab | SelectionMode.OnlyUserModifiable);
{
+
       
foreach (Transform transform in transforms)
+
        if (transforms.Length > 0 && EditorUtility.DisplayDialog("Place Selection On Surface?",
{
+
            "Are you sure you want to place " + transforms.Length +
RaycastHit hit;
+
            ((transforms.Length > 1) ? " objects" : " object") +
if (Physics.Raycast(transform.position, Vector3.down, out hit))
+
            " on the surface in the -Y direction?", "Place", "Do Not Place"))
{
+
        {
transform.position = hit.point;
+
            foreach (Transform transform in transforms)
Vector3 randomized = Random.onUnitSphere;
+
            {
randomized = new Vector3(randomized.x, 0F, randomized.z);
+
                RaycastHit hit;
transform.rotation = Quaternion.LookRotation(randomized, hit.normal);
+
                if (Physics.Raycast(transform.position, Vector3.down, out hit))
}
+
                {
}
+
                    transform.position = hit.point;
}
+
                    Vector3 randomized = Random.onUnitSphere;
}
+
                    randomized = new Vector3(randomized.x, 0F, randomized.z);
 +
                    transform.rotation = Quaternion.LookRotation(randomized, hit.normal);
 +
                }
 +
            }
 +
        }
 +
    }
 
  }
 
  }
 +
</syntaxhighlight>

Latest revision as of 20:45, 10 January 2012

Author: Jonathan Czeck (aarku)

[edit] Description

This wizard takes the current selection and raycasts downward from each object and moves them to where the raycast hit. It is useful for placing a lot of items on top of a terrain.

It is not bulletproof to say the least, but it provides a starting point.

[edit] Usage

Place this script in YourProject/Assets/Editor and a menu item will automatically appear in the GameObject menu after it is compiled.

[edit] C# - PlaceSelectionOnSurface.cs

 using UnityEngine;
 using UnityEditor;
 
 public class PlaceSelectionOnSurface : ScriptableObject
 {
    [MenuItem ("GameObject/Place Selection On Surface")]
    static void CreateWizard ()
    {
        Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep |
            SelectionMode.ExcludePrefab | SelectionMode.OnlyUserModifiable);
 
        if (transforms.Length > 0 && EditorUtility.DisplayDialog("Place Selection On Surface?",
            "Are you sure you want to place " + transforms.Length +
            ((transforms.Length > 1) ? " objects" : " object") +
            " on the surface in the -Y direction?", "Place", "Do Not Place"))
        {
            foreach (Transform transform in transforms)
            {
                RaycastHit hit;
                if (Physics.Raycast(transform.position, Vector3.down, out hit))
                {
                    transform.position = hit.point;
                    Vector3 randomized = Random.onUnitSphere;
                    randomized = new Vector3(randomized.x, 0F, randomized.z);
                    transform.rotation = Quaternion.LookRotation(randomized, hit.normal);
                }
            }
        }
    }
 }
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox