- Home /
Texture2D on Property Drawers.
i have this scripts.
Item class
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Item {
public string itemName;
public Texture2D itemImage;
public int itemPrice;
}
ItemDrawer class
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer (typeof (ItemAttribute))]
public class ItemDrawer : PropertyDrawer {
ItemAttribute item { get { return ((ItemAttribute)attribute); } }
public override void OnGUI (Rect position, SerializedProperty prop, GUIContent label) {
// Adjust height of the text field
EditorGUI.BeginProperty (position, label, prop);
// Draw label
//position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);
// Don't make child fields be indented
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
// Calculate rects
//var amountRect = new Rect (position.x, position.y, 30, position.height);
//var unitRect = new Rect (position.x+35, position.y, 50, position.height);
//var nameRect = new Rect (position.x+90, position.y, position.width-90, position.height);
// Draw fields - passs GUIContent.none to each so they are drawn without labels
//EditorGUI.PropertyField (amountRect, prop.FindPropertyRelative ("itemImage"), GUIContent.none);
//EditorGUI.PropertyField (unitRect, prop.FindPropertyRelative ("itemPrice"), GUIContent.none);
//EditorGUI.PropertyField (nameRect, prop.FindPropertyRelative ("itemName"), GUIContent.none);
//var texture = (Texture2D)prop.objectReferenceValue;
//EditorGUI.LabelField (new Rect (position.x, position.y, position.width, position.height), "Image Preview", GUIContent);
//EditorGUI.LabelField(new Rect(position.x,position.y,position.width,position.height), texture, GUIContent(Texture2D));
//EditorGUI.DrawPreviewTexture(new Rect(position.x,position.y,position.width,position.height)
// Set indent back to what it was
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty ();
}
}
ItemAttribute class
using UnityEngine;
public class ItemAttribute : PropertyAttribute {
public ItemAttribute () {
}
}
example of use
[ItemAttribute]
public Item item;
What i want: OBS: achieve this using custom inspector but i want property drawers.
so as u can see that the problem i having i want to make the same preview texture with all the infos using property drawes but having hard time trying to get image texture2D to show and make it selectable so users can change the sprite right there on inspector. help please.
i already try this ways of get texture.
var texture = (Texture2D)prop.objectReferenceValue;
EditorGUI.PropertyField (amountRect, prop.FindPropertyRelative ("itemImage"), GUIContent.none);
Your answer
Follow this Question
Related Questions
Select a readonly value from dropdown using PropertyDrawers 0 Answers
PropertyDrawer not updating in custom inspector. 0 Answers
CustomAttribute on Class or Method ( EDIT: Button to call methods) 2 Answers
SerializedProperty to Texture2D 2 Answers
What should be handled in the custom inspector version of a script? 1 Answer