Search Unity

Proper way to disable transform script?

Discussion in 'Editor & General Support' started by MartinOrtiz, Dec 10, 2018.

  1. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    I have a basic transform script that rotates a 3D object, it's a 3D medical DICOM "image" of a skull.

    I have set up slider controls, to adjust brightness and other stuff.

    Now when I click and move the thumb of the slider control, my transform script is still in effect and is rotating the 3D skull, as I scrub back and forth the thumb of the slider.

    What is the proper way to disable the transform script or script action, until I stop using the slider?
     
  2. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    I know how to enable/disable stuff from Unity3D environment....

    I need to disable the transform script when I am using the slider. When I stop using the slider, I want to enable script.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Did you even click the link from @LurkingNinjaDev ? It is exactly what you are asking for.
     
  4. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    When I click the slider, I want the script turned off.
    When I release the mouse button, I want the script turned on.
    I know how to write the script to turn it on and off....that's not the issue...
    I need to turn it off when I actually click on the slider, there is no "mouse down" event on the slider, at least that I know of. And no "mouse up" event on the slider.
     
  5. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
  6. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    How do I use a Custom Slider ....like below....?
    I don't mean how to create the script file, how do I add this slider to a canvas in Unity3D like a regular slider?

    Declare a new class that inherits from Slider class and overriding the OnPointerDown() method like this:


    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.EventSystems;

    public class CustomSlider: Slider
    {
    public override void OnPointerDown(PointerEventData eventData)
    {
    base.OnPointerDown(eventData);

    // Your code here
    }
    }
     
  7. https://docs.unity3d.com/ScriptReference/UI.Slider.html

    RTFM. Check out the OnPointerEnter and OnPointerExit. (Or OnPointerDown and OnPointerUp if you want enable/disable only on click).

    Just write your class and attach it like a normal Slider on the GameObject instead of a normal Slider. I don't understand what you don't understand.
     
    Joe-Censored likes this.
  8. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    How do I get the derived class of the slider to appear in the Unity3D work space viewport?
     
  9. Well, probably you should go back to the basic tutorials. You're clearly not ready to work on your own yet.

    Insert a slider, delete the old slider script, attach your own derived script.
     
  10. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    I have

    ....
    public class CustomSlider : Slider
    {
    ......
    public override void OnPointerDown(PointerEventData eventData)
    {
    ......
    }
    }
     
  11. And?
     
  12. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    You say to attach it to a GameObject...which one?

    To the canvas? I tried, when I run app, the derived slider does not show up.
    Dragging to Canvas, #1, adds it as a component of Canvas, but it does not appear as a child slider node of canvas, nor does it appear in 2D viewport, #2, so that I can adjust its location etc...

     
  13. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    I understand derived classes...etc...I am familiar with C#....so that's not the issue.

    I understand how to attach scripts/classes to objects, that's not the issue...

    It's a slider, a derived one, so I can't just attach this to anything per say, and expect to see the slider when I run the app.

    I hope my quandary makes sense..
     
  14. Okay I will go full beginner:

    - Select one of your existing sliders on the scene.
    - look for the "Slider" component
    - Delete it
    - Add your own Slider script
    - change the parameters to your liking
    - rinse and repeat
     
  15. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    Is there a way to get the derived slider to appear in the Unity3D scene view port?
     
  16. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    Oh...didn't know I could do that....
     
  17. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    That's what I was trying to do....thanks a lot, it works.
     
    Lurking-Ninja likes this.
  18. Please delete the attachment and put your code in code tags. You can find the button in the toolbar when you're editing your post.
     
  19. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    that is, when I run the app, and try to drag the slider thumb, it does not move.

    The custom slider appears in scene hierarchy and scene viewport, with child property nodes like a normal slider has....
     
  20. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class CustomSlider : Slider
    6. {
    7.     private Transformer MyTransformer;
    8.  
    9.     protected override void Start()
    10.     {
    11.         base.Start();
    12.  
    13.         GameObject varGameObject = GameObject.Find("DataCube");
    14.         //Transformer varGameObject2 = varGameObject.GetComponent<Transformer>();
    15.         MyTransformer = GameObject.Find("DataCube").GetComponent<Transformer>();
    16.     }
    17.  
    18.     //Do this when the mouse is clicked over the selectable object this script is attached to.
    19.     public override void OnPointerDown(PointerEventData eventData)
    20.     {
    21.         base.OnPointerDown(eventData);
    22.         MyTransformer.enabled = false;
    23.     }
    24.  
    25.     //Do this when the mouse is clicked over the selectable object this script is attached to.
    26.     public override void OnPointerUp(PointerEventData eventData)
    27.     {
    28.         base.OnPointerUp(eventData);
    29.         MyTransformer.enabled = true;
    30.     }
    31.  
    32. }
    33.  
    34.  
     
  21. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    Shows up like normal slider now...but when running, the slider does not work properly, I can't move thumb.

     
  22. I can't see the screenshot very well, have you filled the properties in the inspector like the others are?
    Do you have any errors in the console? Your class seems good enough.
     
  23. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    I'll check...
     
  24. MartinOrtiz

    MartinOrtiz

    Joined:
    Apr 12, 2018
    Posts:
    122
    OK, some fields were not set up properly in the inspector, after I fixed that it works now when I run the app.

    thanks for help!
     
    Lurking-Ninja likes this.