Supporting Cancel

What if you move or scale or otherwise change the picture and don't like it? The toolbar has a Done button; we should add a Cancel button. Cancel will reset the picture parameters to the values it had before this editing session started. This will be handed in the PictureController component.

Presently, the attributes of the picture we've been editing are FramedImage Transform. In Unity, you need to save them as three separate values for position, rotation, and scale. We will save the starting transform values when we begin editing, and restore them if we cancel editing.

At the top of the class, add the following to PictureController:

File: PictureController.cs
private Vector3 startPosition; private Vector3 startScale; private Quaternion startRotation;

Then add these helper functions:

    private void SavePictureProperties() { 
        startPosition = transform.localPosition; 
        startScale = transform.localScale; 
        startRotation = transform.localRotation; 
    } 
 
    private void RestorePictureProperties() { 
        transform.localPosition = startPosition; 
        transform.localScale = startScale; 
        transform.localRotation = startRotation; 
    } 

Call the SavePictureProperties from BeginEdit:

    private void BeginEdit() { 
        SavePictureProperties(); 
        toolbar.SetActive(true); 
    } 

Add a new function, CancelEdit:

    private void CancelEdit() { 
        RestorePictureProperties(); 
        toolbar.SetActive(false); 
    } 

In the Execute function, add a case for CANCEL as follows:

            case PictureCommand.CANCEL: 
                CancelEdit(); 
                break; 

Save the PictureController.cs script.

Go back to Unity. Add the CancelButton and set up its PictureTool as follows:

  1. From Assets/SimpleIcons/Prefabs/ drag the CancelButton prefab into the Hierarchy as a child of Toolbar.
  2. Set its Position X to 0.3.
  3. Add the PictureAction component to the DoneButton object in the Toolbar.
  4. Set its Command to CANCEL.

Try it out. Press Play, modify the picture, then press Cancel in the toolbar. The picture should return to its pre-edit version.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.248.208