Last active
October 25, 2015 10:22
-
-
Save mmj-the-fighter/33c9284c0dc360cfd076 to your computer and use it in GitHub Desktop.
Unity3D Editor Script: Moves selected object downwards till it touches the ground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copyright © 2015 Manoj M J | |
//All Rights Reserved | |
//Why I do copyright the code that I provide at gist.github.com | |
//( https://gist.github.com/mmj-the-fighter/bccd0a7ff57c638beee8 ) | |
/* | |
* DropObject.cs - Moves selected object downwards till it touches the ground | |
* This is useful for placing 3d models on scene. | |
* TODO:Add Spherecast in addition to Raycast | |
* (c)mmj 2015 | |
*/ | |
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class DropObject : ScriptableObject { | |
static bool optionMoveUpToDropHeightAndThenDrop = true; | |
static bool optionRayCastAgainstAllColliders = false; | |
static int groundLayerNumber = 8;//CAUTION:Remember to change the value of this variable"groundLayerNumber" with your data | |
static float expectedHeightOffsetAfterDrop = 0.5f; | |
static float dropHeight = 1000.0f; | |
internal struct UndoInfo | |
{ | |
public Transform transform; | |
public Vector3 position; | |
}; | |
static List<UndoInfo> undoInfoList = new List<UndoInfo>(); | |
static UndoInfo undoInfo; | |
public enum MethodID | |
{ | |
MethodNone, | |
MethodDropSingleObject, | |
MethodDropSelectedObjects, | |
MethodDropChildObjects | |
}; | |
static MethodID recentlyExecutedMethodsID = MethodID.MethodNone; | |
static void Drop(Transform target) { | |
if(optionMoveUpToDropHeightAndThenDrop) { | |
target.position = target.position + new Vector3(0.0f,dropHeight,0.0f); | |
} | |
Vector3 origin = target.position; | |
Vector3 direction = Vector3.down; | |
RaycastHit hitInfo = new RaycastHit(); | |
int layerMask = 1 << groundLayerNumber; | |
if(optionRayCastAgainstAllColliders) { | |
Physics.Raycast(origin,direction,out hitInfo); | |
} else { | |
Physics.Raycast(origin,direction,out hitInfo,Mathf.Infinity,layerMask); | |
} | |
target.position = hitInfo.point + new Vector3(0.0f,expectedHeightOffsetAfterDrop,0.0f); | |
} | |
[ MenuItem( "Tools/DropObject/DropSingleObject" ) ] | |
static void DropSingleObject () { | |
Transform activeTransform = Selection.activeTransform; | |
undoInfo.transform = activeTransform; | |
undoInfo.position = activeTransform.position; | |
Drop (activeTransform); | |
recentlyExecutedMethodsID = MethodID.MethodDropSingleObject; | |
} | |
[ MenuItem( "Tools/DropObject/DropSelectedObjects" ) ] | |
static void DropSelectedObjects() { | |
undoInfoList.Clear(); | |
foreach (Transform t in Selection.transforms) { | |
UndoInfo undoInformation = new UndoInfo(); | |
undoInformation.transform = t; | |
undoInformation.position = t.position; | |
undoInfoList.Add(undoInformation); | |
Drop (t); | |
} | |
recentlyExecutedMethodsID = MethodID.MethodDropSelectedObjects; | |
} | |
[ MenuItem( "Tools/DropObject/DropChildObjects" ) ] | |
static void DropChildObjects() { | |
undoInfoList.Clear(); | |
Transform activeTransform = Selection.activeTransform; | |
foreach (Transform child in activeTransform) { | |
UndoInfo undoInformation = new UndoInfo(); | |
undoInformation.transform = child; | |
undoInformation.position = child.position; | |
undoInfoList.Add(undoInformation); | |
Drop (child); | |
} | |
recentlyExecutedMethodsID = MethodID.MethodDropChildObjects; | |
} | |
[ MenuItem( "Tools/DropObject/Undo" ) ] | |
static void Undo() { | |
switch(recentlyExecutedMethodsID) | |
{ | |
case MethodID.MethodDropSingleObject: | |
UndoDropSingleObject(); | |
break; | |
case MethodID.MethodDropSelectedObjects: | |
UndoDropSelectedObjects(); | |
break; | |
case MethodID.MethodDropChildObjects: | |
UndoDropChildObjects(); | |
break; | |
} | |
} | |
static void UndoDropSingleObject () { | |
undoInfo.transform.position = undoInfo.position; | |
} | |
static void UndoDropSelectedObjects () { | |
foreach (UndoInfo undoInformation in undoInfoList) { | |
undoInformation.transform.position = undoInformation.position; | |
} | |
} | |
static void UndoDropChildObjects () { | |
foreach (UndoInfo undoInformation in undoInfoList) { | |
undoInformation.transform.position = undoInformation.position; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment