SetFrame in PictureController

The PictureController will expose a method SetFrame() that takes a frame game object as a parameter. It will delete the existing frame in the FramedImage and spawn (add) the selected one instead. Make the following additions to the PictureController.cs script.

At the top of the class, add a variable for the frame spawn:

File: PictureController.cs
private Renderer imageRenderer;

Initialize this in Start(). We already have a reference to the framedImage, so it's safe to just find its child Image:

    void Start() { 
        ... 
        Transform image = framedImage.Find("Image"); 
        imageRenderer = image.gameObject.GetComponent<Renderer>(); 

Now we can write the public function SetFrame as follows:

    public void SetFrame(GameObject frameGameObject) { 
        GameObject currentFrame = GetCurrentFrame(); 
        if (currentFrame != null) 
            Destroy(currentFrame); 
 
        GameObject newFrame = Instantiate(frameGameObject, frameSpawn); 
        newFrame.transform.localPosition = Vector3.zero; 
        newFrame.transform.localEulerAngles = Vector3.zero; 
        newFrame.transform.localScale = Vector3.one; 
    } 
 
    private GameObject GetCurrentFrame() { 
        Transform currentFrame = frameSpawn.GetChild(0); 
        if (currentFrame != null) { 
            return currentFrame.gameObject; 
        } 
        return null; 
    } 

We also added a helper function to get the current frame as the first (and only) child of FrameSpawn. SetFrame gets the current frame and deletes it. Then it creates a new frame based on the one selected from the menu and resets its transform.

The next step is to build the menu itself.

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

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