Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Berenger · Jan 11, 2011 at 10:11 AM · raycastcustom-inspectoronscenegui

How to choose the transform tool from script ?

Is it possible to control the current transform tool (Pan, translate, rotate or scale) with a simple line like

Editor.transformTool = TransformTool.Rotate;

The reason I want to do this is I'm doing a custom editor script where you can click a button in the inspector, then an object in the scene view, which will affect the object to some variable. If the transform tool is different from pan, the click is ignored by my script and the object is simply selected. I hear there is the function Event.current.Use();, but it doesn't seems to help.

[ EDIT ] Just to be clear, here is the code I'm talking about, without useless stuff :

class ObjectEditor extends Editor { private var pick = false; private var selection : GameObject = null;

 function OnInspectorGUI () 
 {       
     var str : String = selection ? selection.name : "null";

     // Create a button in the inspector with the name f the selection displayed in it.
     // If you click it, you enter the picking mode and the next click in the scene view will pick an object.
     GUILayout.BeginHorizontal ();
         var r : Rect = EditorGUILayout.BeginHorizontal ("Button");
             if (GUI.Button (r, GUIContent.none))
                 pick = !pick;               

             GUILayout.Label (str);
         EditorGUILayout.EndHorizontal ();
     GUILayout.EndHorizontal ();
 }

 function OnSceneGUI()
 {
     // If this is a click and if we are in picking mode
     // The problem is that if you click in the scene view, you loose
     // the focus on your object, so this code isn't executed.
     if( Event.current.type == EventType.MouseUp && pick )
     {
         // We are going to cast a ray through the scene to pick an object.
         var R : Ray =  HandleUtility.GUIPointToWorldRay( Event.current.mousePosition );
         var H : RaycastHit;

         if( Physics.Raycast( R, H, Mathf.Infinity ) )
         {
             pick = false;
             selection = H.collider.gameObject;
             Repaint(); // Display the name right now.
         }

         // Supposed to "eat" the event, prenting it to do something else. I guess the selection by Unity occurs before.
         Event.current.Use();
     }
 }

}

[ EDIT 2 ] To be thorough, OnSceneGUI is executed once with a MouseDown in any cases. But then, the focus is lost.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeffom · Jan 11, 2011 at 12:55 PM 0
Share

Do you really need 1 button to do all that? you simply could add 3 buttons, 1 for each transformation, and the associate the button to the transformation you want into the object you selected.

avatar image Berenger · Jan 11, 2011 at 10:06 PM 2
Share

You get me wrong. I just want to be able to click in the scene view without loosing my selection. It appears that, in pan mode and only that one, the click doesn't select objects, which is convenient for me. I want to say "Ok, my next click won't be a selection, so unity, put yourself in pan mode and don't do anything stupid !"

avatar image Bunny83 · Jan 13, 2011 at 04:50 AM 0
Share

I've come across a similar problem, i just want to know the current mode of transformation. The local/global or pivot/center setting would also be nice to "get"/"set".

avatar image yoyo · Jan 13, 2011 at 04:56 PM 0
Share

How did you try using Event.current.Use? You would need to get mouse input in OnSceneGUI and handle your own picking logic, then call Event.current.Use() so Unity doesn't use the mouse click again to select the object.

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by yoyo · Jan 18, 2011 at 05:20 PM

By digging into the UnityEditor DLL with MonoDevelop, I came up with the following.

DISCLAIMER: The following script uses Reflection to call a private API. It is not documented or supported by Unity and may change in a future release. (Some may consider this evil; you have been warned.)

Put this C# script into your Editor folder ...

using System.Reflection;

using UnityEngine; using UnityEditor;

// SceneToolHacker uses diabolical reflection technique to set the // active tool. Note that this may well break with future Unity releases!! public static class SceneToolHacker { public enum Tool { DragCamera = 0, Translate = 1, Rotate = 2, Scale = 3 }

 public static Tool CurrentTool
 {
     get { return (Tool)mTools_current.GetValue(null, null); }
     set { mTools_current.SetValue(null, (int)value, null); }
 }

 // "Sorry Virginia, there is no private."
 private static PropertyInfo mTools_current = typeof(Tools).GetProperty("current", BindingFlags.Static | BindingFlags.NonPublic);

}

You can either take it from there, or give this menu demo a whirl to see it in action ...

using System;

using UnityEngine; using UnityEditor;

// Quick menu-driven demonstration. public static class SceneToolHackerMenu { [MenuItem("Hack/Scene Tool/Get")] public static void GetSceneTool() { Debug.Log(String.Format("{0} tool is active.", SceneToolHacker.CurrentTool)); }

 [MenuItem("Hack/Scene Tool/Set/Translate")]
 public static void SetSceneToolTranslate()
 {
     SceneToolHacker.CurrentTool = SceneToolHacker.Tool.Translate;
 }

 [MenuItem("Hack/Scene Tool/Set/Rotate")]
 public static void SetSceneToolRotate()
 {
     SceneToolHacker.CurrentTool = SceneToolHacker.Tool.Rotate;
 }

 [MenuItem("Hack/Scene Tool/Set/Scale")]
 public static void SetSceneToolScale()
 {
     SceneToolHacker.CurrentTool = SceneToolHacker.Tool.Scale;
 }

}

Dr. EvilEvil is fun!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Jan 19, 2011 at 08:38 AM 0
Share

Wahou, it works perfectly (with the current version at least) ! I wanted to use those function in a monobehaviour (well, in it's custom editor) and for those who are interested, you can call them with EditorApplication.Execute$$anonymous$$enuItem ("Hack/Scene Tool/Set/..."). Thanks a lot :)

avatar image yoyo · Jan 19, 2011 at 05:15 PM 0
Share

Or you can use "SceneToolHacker.CurrentTool = SceneToolHacker.Tool.Translate;"

avatar image hardwire · Feb 21, 2011 at 01:03 PM 2
Share

As of 3.2 the property was made public, so now there is no need to use reflection. You can access it directly as Tools.current

avatar image yoyo · Feb 21, 2011 at 05:18 PM 0
Share

excellent, good to know :)

avatar image
0
Best Answer

Answer by yoyo · Jan 13, 2011 at 10:04 AM

Try setting ...

gameObj.hideFlags = HideFlags.NotEditable;

This should make the object non-selectable in the scene view, which could solve your problem. (Though I'm not quite clear if the object being selected is the problem you're having, so this may not help.)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Jan 13, 2011 at 10:56 AM 0
Share

Not really, my problem is that I loose the focus when you click an object in the scene view. I want to remain in the same object (which have the script I care about) and just point out other objects to store in an array.

avatar image yoyo · Jan 13, 2011 at 04:54 PM 0
Share

Ah, ok, that makes sense.

avatar image
0
Best Answer

Answer by Berenger · Jan 17, 2011 at 09:02 AM

So, I didn't find out the solution, but I found an other way for my problem (and actually a better way). It's pretty simple, just need to use the right click instead of the left ...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Linphency · Dec 22, 2011 at 08:03 AM

This script works for me in 3.4

Tools.current = Tool.View;

Tool enum class can be found here

http://unity3d.com/support/documentation/ScriptReference/Tool.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

[Custom Inspector] Selecting Gizmo in Scene View and issues 1 Answer

Scripting a Click event in the Unity Editor 1 Answer

Triangles Index always zero - Custom Editor 1 Answer

Display OnSceneGUI without selecting the object 0 Answers

OnSceneGUI has odd behaviour with multiple objects selected 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges