forked from autocore-ai/MapToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
134 lines (131 loc) · 5.69 KB
/
Utils.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#region License
/******************************************************************************
* Copyright 2018-2021 The AutoCore Authors. All Rights Reserved.
*
* Licensed under the GNU Lesser General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/lgpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#endregion
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using UnityEditor;
using UnityEngine;
namespace Packages.MapToolbox
{
static class Utils
{
public static void TryAdd<T>(this ICollection<T> collection, T target)
{
if (!collection.Contains(target))
{
collection.Add(target);
}
}
public static void TryRemove<T>(this ICollection<T> collection, T target)
{
if (collection.Contains(target))
{
collection.Remove(target);
}
}
public static void RemoveNull<T>(this List<T> list) => list.RemoveAll(_ => _ == null);
public static void RemoveLast<T>(this IList<T> list)
{
if (list.Count > 0)
{
list.RemoveAt(list.Count - 1);
}
}
public static void DestroyAll<T>(this ICollection<T> collection) where T : Object
{
for (int i = 0; i < collection.Count; i++)
{
Undo.DestroyObjectImmediate(collection.ElementAt(i));
}
}
public static void RecordUndoCreateGo(this GameObject go) => Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
public static T GetChild<T>(this Component component, string name = null) where T : Component
{
T ret = component.GetComponentInChildren<T>();
if (ret == null)
{
ret = component.AddChildGameObject<T>(name);
}
return ret;
}
public static T AddChildGameObject<T>(this Component component, string name = null) where T : Component
{
var target = new GameObject(name ?? typeof(T).Name).AddComponent<T>();
target.transform.SetParent(component.transform);
target.transform.position = component.transform.position;
return target;
}
public static string ChildMapId(this Transform transform) => (transform.childCount + 1).ToString();
public static void SetLocalX(this Transform transform, double x) => transform.SetLocalX((float)x);
public static void SetLocalY(this Transform transform, double y) => transform.SetLocalY((float)y);
public static void SetLocalZ(this Transform transform, double z) => transform.SetLocalZ((float)z);
public static void SetLocalX(this Transform transform, float x) => transform.localPosition = new Vector3(x, transform.localPosition.y, transform.localPosition.z);
public static void SetLocalY(this Transform transform, float y) => transform.localPosition = new Vector3(transform.localPosition.x, y, transform.localPosition.z);
public static void SetLocalZ(this Transform transform, float z) => transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, z);
public static Vector3 MousePointInSceneView
{
get
{
Vector3 mousePosition = Event.current.mousePosition;
mousePosition.y = SceneView.lastActiveSceneView.camera.pixelHeight - mousePosition.y * EditorGUIUtility.pixelsPerPoint;
mousePosition.x *= EditorGUIUtility.pixelsPerPoint;
mousePosition.z = 20;
mousePosition = SceneView.lastActiveSceneView.camera.ScreenToWorldPoint(mousePosition);
return mousePosition;
}
}
public static bool MouseInSceneView => SceneView.lastActiveSceneView.camera.pixelRect.Contains(Event.current.mousePosition * EditorGUIUtility.pixelsPerPoint);
public static XmlElement AddTag(this XmlDocument doc, string key, string value)
{
XmlElement tag = doc.CreateElement("tag");
tag.SetAttribute("k", key);
tag.SetAttribute("v", value);
return tag;
}
public static XmlElement AddMember(this XmlDocument doc, string type, string @ref, string role)
{
XmlElement member = doc.CreateElement("member");
member.SetAttribute("type", type);
member.SetAttribute("ref", @ref);
member.SetAttribute("role", role);
return member;
}
public static T GetOrAddComponent<T>(this Component component) where T : Component
{
var com = component.GetComponent<T>();
if (com == null)
{
com = component.gameObject.AddComponent<T>();
}
return com;
}
public static float GetHeight(Vector3 position)
{
const float maxHeight = 10000;
var from = new Vector3(position.x, maxHeight, position.z);
if (Physics.Raycast(new Ray(from, Vector3.down), out RaycastHit hit, maxHeight * 2))
{
return hit.point.y + 0.1f;
}
else
{
return 0;
}
}
}
}