Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NEED SMART PERSON! I'm working on an editor script!

Discussion in 'Scripting' started by keenanwoodall, Jul 21, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I'm really early in the process of making a custom sculpting tool and I'm not sure I'm doing it right. I'm using a sphere as the indicator of where you will be sculpting and I can't figure out how to make it work while I'm clicking. Is there a way to increase the update speed for OnSceneGUI? Also, when I change the tool size slider, it doesn't update the sphere's size. Whenever I deselect the object, it resets enableSculpting to false. There's also a few more problems that you might see in my script. Here it is:
    Code (js):
    1.  
    2. @CustomEditor (SculptableObject)
    3.  
    4. class Sculpt extends Editor
    5. {
    6.     var enableSculpting : boolean;
    7.     var toolsize : float;
    8.     private var createSculptSphere : boolean = false;
    9.     private var sculptSphere : GameObject = Resources.Load ("KeenansCoolResources/KeenansCoolSculptingStuff/_SculptSphere");
    10.     private var editorSculptSphere : GameObject;
    11.     function OnInspectorGUI ()
    12.     {
    13.         toolsize = EditorGUILayout.Slider (toolsize, 0.1, 30);
    14.         enableSculpting = EditorGUILayout.Toggle (enableSculpting);
    15.  
    16.         editorSculptSphere = GameObject.Find("_SculptSphere(Clone)");
    17.     }
    18.     function OnSceneGUI ()
    19.     {
    20.         if (Selection.activeGameObject && enableSculpting)
    21.         {
    22.             editorSculptSphere = GameObject.Find("_SculptSphere(Clone)");
    23.             var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
    24.             var hit : RaycastHit;
    25.             if (Physics.Raycast(ray,hit, 500))
    26.             {
    27.                 if (hit.transform.gameObject == Selection.activeGameObject)
    28.                 {
    29.                     if (!GameObject.Find("_SculptSphere(Clone)"))
    30.                     {
    31.                         Instantiate (sculptSphere, hit.point, Quaternion.identity);
    32.                     }
    33.                     else
    34.                     {
    35.                         editorSculptSphere.transform.position = hit.point;
    36.                     }
    37.                 }
    38.                 else if (hit.transform.gameObject != Selection.activeGameObject || hit.point == null)
    39.                 {
    40.                     DestroyImmediate (editorSculptSphere);
    41.                 }
    42.             }
    43.         }
    44.         else if (!Selection.activeGameObject || !Selection.activeGameObject.GetComponent(SculptableObject) || Selection.activeGameObject == editorSculptSphere)
    45.         {
    46.             if (!Input.GetMouseButtonDown(0) && !Input.GetMouseButtonUp(0) && !Input.GetMouseButton(0))
    47.             {
    48.                 editorSculptSphere = GameObject.Find("_SculptSphere(Clone)");
    49.                 DestroyImmediate (editorSculptSphere);
    50.                 return;
    51.             }
    52.         }
    53.     }
    54. }
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Use SceneView.Repaint (); to update the scene. Just chuck it in your on scene GUI
     
  3. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Thanks. One problem down, a bunch to go!
     
  4. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Actually, I get an error. Idk why, but I always forget how to deal with non static error stuff. Here's the error:

    Assets/Editor/KeenansCoolToolsScripts/KeenansCoolSculpter.js(34,27): BCE0020: An instance of type 'UnityEditor.EditorWindow' is required to access non static member 'Repaint'.
     
  5. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Oh sorry, I meant SceneView.RepaintAll ();
     
  6. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    That should work (I hope) ha
     
  7. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Thanks, the sculpting sphere doesn't stay at wrong times, and it updates faster! Do you know why changing the tool size via slider doesn't change the spheres size?
     
  8. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I don't know an exact reason no; Maybe Lightstriker will; he's an editor beast. But I imagine it's simply because they don't run at the same time. When your mouse is clicked onto the inspector the inspector updates, when your mouse is in scene view the scene view updates.

    If you added the SceneView.RepaintAll () in OnInspectorGUI () the sphere would update accordingly
     
  9. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Frankly, I try to stay away from JS snippet, since I don't write in that and I'm using Visual Studio.

    As for the tool size, I see you assign a variable named "toolsize", but never do anything with it... Unless the first snippet is really not up to date. You should probably do something like;

    Code (csharp):
    1. editorSculptSphere.transform.localScale = new Vector3(toolsize, toolsize, toolsize);
     
  10. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Thanks guys! I'l try that out and let yah know if it works