Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Lock RectTransform's fields

Tags:

c#

unity3d

unity5

I'm creating custom layout group and I want to control RectTransform on the child Objects. I want to lock some fields on child's RectTransform like when using canvas or Unity's Horizontal or Vertical group so that it cannot be modified.

I need the same effect. You can see this message on top of child's RectTransform : Some values driven by HorizontalLayoutGroup

enter image description here

I find a halfway :

Adding [ExecuteInEditMode] then:

public void Update()
{
#if UNITY_EDITOR
    if (!Application.isPlaying)
    {
        /* Todo => update child's positions here. */
    }
#endif
}

Any other idea?

like image 919
High Avatar asked Mar 05 '18 14:03

High


1 Answers

This is done with the DrivenRectTransformTracker API.

From the doc:

Driving a RectTransform means that the values of the driven RectTransform are controlled by that component. These driven values cannot be edited in the Inspector (they are shown as disabled). They also won't be saved when saving a scene, which prevents undesired scene file changes.

Whenever the component is changing values of driven RectTransforms, it should first call the Clear method and then use the Add method to add all RectTransforms it is driving. The Clear method should also be called in the OnDisable callback of the component.

No example from the doc but below is how to use it:

public RectTransform targetRC;
UnityEngine.Object driver;

void Start()
{
    DrivenRectTransformTracker dt = new DrivenRectTransformTracker();
    dt.Clear();

    //Object to drive the transform
    driver = this;
    dt.Add(driver, targetRC, DrivenTransformProperties.All);
}

The RectTransform linked to the targetRC variable will now be locked and cannot be modified from the Editor. It should now say something like "Some values are driven by another object". You can use DrivenTransformProperties to specify which variables to lock.

This is what it looks like after executing this code:

enter image description here

like image 90
Programmer Avatar answered Sep 28 '22 04:09

Programmer