GUITexture' is obsolete: 'GUITexture has been removed. Use UI.Image instead.'

Hi,

I am trying to use an asset on Unity 2019.3.0f3 which is not compatible with GUITexture anymore, but I really need this asset and cannot seem to fix the error.
Here’s a sample of the code:

// simple GUITexture dragbar for brush size

using UnityEngine;
using System.Collections;

namespace unitycoder_MobilePaint
{

    public class AdjustBrushSize : MonoBehaviour {

        public GameObject painter; // our main paint plane reference

        public GUITexture indicator; // current size indicator
        private int minSize = 1; // min brush radius
        private int maxSize = 64; // max brush radius
        private float sizeScaler = 1; // temporary variable to calculate scale


        // init
        void Start ()
        {
            if (painter==null)
            {
                Debug.LogError("Painter gameObject not found - Have you assigned it?");
            }

            // calculate current indicator position
            minSize = painter.GetComponent<unitycoder_MobilePaint.MobilePaint>().brushSizeMin;
            maxSize = painter.GetComponent<unitycoder_MobilePaint.MobilePaint>().brushSizeMax;
            sizeScaler = maxSize/GetComponent<GUITexture>().pixelInset.height;
//            float borderOffsetY = (painter.GetComponent<unitycoder_MobilePaint.MobilePaint>().brushSize-1)/sizeScaler+guiTexture.pixelInset.y;
            float borderOffsetY = (painter.GetComponent<unitycoder_MobilePaint.MobilePaint>().brushSize-1-minSize)/sizeScaler+GetComponent<GUITexture>().pixelInset.y;
            indicator.pixelInset = new Rect(indicator.pixelInset.x,borderOffsetY,indicator.pixelInset.width,indicator.pixelInset.height);
        }


        // guitexture is dragged, update indicator position & brush size variable in painter gameobject
        void OnMouseDrag()
        {
            float borderOffsetY = Mathf.Clamp((int)(Input.mousePosition.y),GetComponent<GUITexture>().pixelInset.y,GetComponent<GUITexture>().pixelInset.y+GetComponent<GUITexture>().pixelInset.height);
            painter.GetComponent<unitycoder_MobilePaint.MobilePaint>().brushSize = (int)Mathf.Clamp( ((borderOffsetY-GetComponent<GUITexture>().pixelInset.y)*sizeScaler), minSize, maxSize);
            indicator.pixelInset = new Rect(indicator.pixelInset.x,borderOffsetY,indicator.pixelInset.width,indicator.pixelInset.height);
        }
    }
}

First, change the type of indicator to “RectTransform”. The two pixelInset lines will need to be something more like:

indicator.position = new Vector3(indicator.position.x, borderOffsetY, indicator.position.z);

(This will probably need some adjustment to look correct after you get it compiling - maybe adding or subtracting from the Y part, etc)

You’ll need to create a Canvas object in the scene, create an Image object as a child of that, and assign it the proper sprite for the indicator. Then you should be able to drag to assign the new Image object as normal.

I also fiind this issue when I am updating my unitu inti 2019.3 version… so I can not play

same issue pixelInset error

The GUITexture component was already marked as “legacy” 10 years ago. So whatever tutorial or asset you’re using, you should look for something else or replace the functionality that was achieved by the GUITexture with an alternative like the mentioned UI.Image. There is no 1-to-1 replacement. If you don’t feel like you can fix / replace the component yourself, we can’t really help you. Especially when you use “some not further specified asset”. StarManta already posted a possible fix. Though it depends on the exact usecase and setup of the asset. You simply can’t use GUITexture anymore. It’s an ancient component which didn’t have great performance anyways.

1 Like

pixelInset is Rect component and you don’t have setter for that…
So replace that rect with set function

uiImage.rectTransform.rect.Set(x,y, width,height)

This would not work as rect is a struct property. As such “reading” the rect would give you a copy on the stack. So calling Set on that Rect would change the values of the copy but would not affect the actual rectTransform. You have to invoke the setter of the rect property. So either do

uiImage.rectTransform.rect = new Rect(x,y, width,height);

or if you just want to set some fields of the Rect, you have to do

Rect rect = uiImage.rectTransform.rect;
rect.x = x; // or whatever. You could use Set here as well, though it wouldn't make much sense.
uiImage.rectTransform.rect = rect;
1 Like