Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by hazem_inspectar, Jan 13, 2020.

  1. hazem_inspectar

    hazem_inspectar

    Joined:
    Jan 10, 2020
    Posts:
    7
    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:
    Code (CSharp):
    1. // simple GUITexture dragbar for brush size
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. namespace unitycoder_MobilePaint
    7. {
    8.  
    9.     public class AdjustBrushSize : MonoBehaviour {
    10.  
    11.         public GameObject painter; // our main paint plane reference
    12.  
    13.         public GUITexture indicator; // current size indicator
    14.         private int minSize = 1; // min brush radius
    15.         private int maxSize = 64; // max brush radius
    16.         private float sizeScaler = 1; // temporary variable to calculate scale
    17.  
    18.  
    19.         // init
    20.         void Start ()
    21.         {
    22.             if (painter==null)
    23.             {
    24.                 Debug.LogError("Painter gameObject not found - Have you assigned it?");
    25.             }
    26.  
    27.             // calculate current indicator position
    28.             minSize = painter.GetComponent<unitycoder_MobilePaint.MobilePaint>().brushSizeMin;
    29.             maxSize = painter.GetComponent<unitycoder_MobilePaint.MobilePaint>().brushSizeMax;
    30.             sizeScaler = maxSize/GetComponent<GUITexture>().pixelInset.height;
    31. //            float borderOffsetY = (painter.GetComponent<unitycoder_MobilePaint.MobilePaint>().brushSize-1)/sizeScaler+guiTexture.pixelInset.y;
    32.             float borderOffsetY = (painter.GetComponent<unitycoder_MobilePaint.MobilePaint>().brushSize-1-minSize)/sizeScaler+GetComponent<GUITexture>().pixelInset.y;
    33.             indicator.pixelInset = new Rect(indicator.pixelInset.x,borderOffsetY,indicator.pixelInset.width,indicator.pixelInset.height);
    34.         }
    35.  
    36.  
    37.         // guitexture is dragged, update indicator position & brush size variable in painter gameobject
    38.         void OnMouseDrag()
    39.         {
    40.             float borderOffsetY = Mathf.Clamp((int)(Input.mousePosition.y),GetComponent<GUITexture>().pixelInset.y,GetComponent<GUITexture>().pixelInset.y+GetComponent<GUITexture>().pixelInset.height);
    41.             painter.GetComponent<unitycoder_MobilePaint.MobilePaint>().brushSize = (int)Mathf.Clamp( ((borderOffsetY-GetComponent<GUITexture>().pixelInset.y)*sizeScaler), minSize, maxSize);
    42.             indicator.pixelInset = new Rect(indicator.pixelInset.x,borderOffsetY,indicator.pixelInset.width,indicator.pixelInset.height);
    43.         }
    44.     }
    45. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    First, change the type of indicator to "RectTransform". The two pixelInset lines will need to be something more like:
    Code (csharp):
    1. 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.
     
  3. zuhdi751

    zuhdi751

    Joined:
    Jan 30, 2020
    Posts:
    1
    I also fiind this issue when I am updating my unitu inti 2019.3 version.. so I can not play
     
  4. usmanahmad7900

    usmanahmad7900

    Joined:
    Jul 26, 2019
    Posts:
    1
    same issue pixelInset error
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,573
    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.
     
    Kurt-Dekker likes this.
  6. speedyracer0311

    speedyracer0311

    Joined:
    Mar 18, 2022
    Posts:
    1
    pixelInset is Rect component and you don't have setter for that..
    So replace that rect with set function

    Code (CSharp):
    1. uiImage.rectTransform.rect.Set(x,y, width,height)
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,573
    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

    Code (CSharp):
    1. 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

    Code (CSharp):
    1. Rect rect = uiImage.rectTransform.rect;
    2. rect.x = x; // or whatever. You could use Set here as well, though it wouldn't make much sense.
    3. uiImage.rectTransform.rect = rect;
     
    Kurt-Dekker likes this.