-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item.cs
75 lines (64 loc) · 2.1 KB
/
Item.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEditor;
using System.Linq;
using System.IO;
using UnityEngine.Rendering;
[System.Serializable]
public class Item : MonoBehaviour
{
[SerializeField] public GameObject go;
[SerializeField] public Texture2D preview;
public GameObject icon;
public Mesh m;
public void Start()
{
Mesh m = null;
MeshCollider mc = go.GetComponent<MeshCollider>();
if (mc)
{
m = go.GetComponent<MeshCollider>().sharedMesh;
} else {
m = go.GetComponent<MeshFilter>().sharedMesh;
}
MeshPreview mp = new MeshPreview(m);
preview = mp.RenderStaticPreview(256, 256);
GameObject icon = FindChild(this.transform.parent.gameObject, "Icon");
icon = FindChild(this.gameObject, "Icon");
icon.GetComponent<RawImage>().texture = preview;
}
public string textureToPNG(Texture2D image)
{
byte[] bytes = image.EncodeToPNG();
var dirPath = Application.dataPath + "/SaveImages2/";
Debug.Log(dirPath);
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
string s = dirPath + go.name + ".png";
File.WriteAllBytes(s, bytes);
return s;
}
public GameObject FindChild(GameObject fromGameObject, string withName)
{
var allKids = fromGameObject.GetComponentsInChildren<Transform>();
var kid = allKids.FirstOrDefault(k => k.gameObject.name == withName);
if (kid == null)
{
Debug.Log("search fail");
return null;
}
return kid.gameObject;
}
public void SetPosition(Transform slotPos)
{
transform.SetParent(slotPos);
transform.SetSiblingIndex(4); //If it is the last => count text will be faded??? do i need this?
transform.localPosition = Vector3.zero;
transform.localScale = Vector3.one;
}
}