2020/06에 해당하는 글 2

유니티 프로젝트 창 아이콘 Custom GUI

유니티 & C#|2020. 6. 30. 00:10
public class GizmoIconUtility
{
	[DidReloadScripts]
	static GizmoIconUtility()
	{
		EditorApplication.projectWindowItemOnGUI = ItemOnGUI;
	}

	static void ItemOnGUI(string guid, Rect rect)
	{
		string assetPath = AssetDatabase.GUIDToAssetPath(guid);
		ItemDefinition item = AssetDatabase.LoadAssetAtPath<ItemDefinition>(assetPath);

		if (item == null ||
			item.Icon == null)
		{
			return;
		}

		rect.height = rect.width;

		EditorGUI.DrawRect(rect, BackgroundColor);
		GUI.DrawTexture(rect, LoadTextureByRarity(item.Rarity));

		var iconRect = new Rect(rect);
		var center = iconRect.center;

		iconRect.width = rect.width * 0.75f;
		iconRect.height = rect.height * 0.75f;
		iconRect.center = center;

		GUI.DrawTexture(iconRect, item.Icon.texture, ScaleMode.ScaleToFit);

		var levelRect = new Rect(rect);
		levelRect.height = 18;

		string type = item.ItemType == ItemType.Weapon ? item.WeaponType.ToString() : item.ItemType.ToString();

		EditorGUI.LabelField(levelRect, $"Lv.{item.ItemLevel} / {type}", EditorStyles.miniBoldLabel);
	}

	static Texture LoadTextureByRarity(ItemRarity rarity)
	{
		switch (rarity)
		{
			case ItemRarity.Common:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Common.png");
			case ItemRarity.Uncommon:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Uncommon.png");
			case ItemRarity.Rare:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Rare.png");
			case ItemRarity.Epic:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Epic.png");
			case ItemRarity.Legendary:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Legendary.png");
			case ItemRarity.Artifact:
				return AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Gizmos/Artifact.png");
		}

		return Texture2D.blackTexture;
	}

	private static Color BackgroundColor => EditorGUIUtility.isProSkin
		? new Color32(56, 56, 56, 255)
		: new Color32(194, 194, 194, 255);

EditorApplication.projectWindowItemOnGUI를 통해 프로젝트 창에서 임의로 아이콘을 그려 넣을 수 있다

댓글()

2D 발사체 포물선 이동

유니티 & C#|2020. 6. 20. 20:18
Vector2 position = transform.position;

Vector2 d = destination - position;
float angle = Vector2.SignedAngle(Vector2.right, d.normalized) + 11.25f;

Vector2 p1 = new Vector2(position.x, 0f);
Vector2 d1 = new Vector2(destination.x, 0f);

float r = Vector2.Distance(p1, d1);
float t = Mathf.Tan(angle * Mathf.Deg2Rad);
float h = destination.y - position.y;

float vx = Mathf.Sqrt(Physics2D.gravity.y * r * r / (2.0f * (h - r * t)));
float vy = t * vx;

Velocity = new Vector2(vx, vy);

댓글()