Skip to content

Commit

Permalink
add misc
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtueSky committed Sep 17, 2023
1 parent 06ad470 commit 786026c
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 0 deletions.
63 changes: 63 additions & 0 deletions VirtueSky/Misc/Common.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;

namespace VirtueSky.Misc
Expand All @@ -25,5 +28,65 @@ public static string DefaultResourcesPath()

return defaultResourcePath;
}

public static bool IsInteger(float value)
{
return (value == (int)value);
}

public static int GetNumberInAString(string str)
{
try
{
var getNumb = Regex.Match(str, @"\d+").Value;
return Int32.Parse(getNumb);
}
catch (Exception e)
{
return -1;
}

return -1;
}

public static void ClearTransform(this Transform transform)
{
var childs = transform.childCount;
for (int i = childs - 1; i >= 0; i--)
{
UnityEngine.Object.DestroyImmediate(transform.GetChild(i).gameObject, true);
}
}

public static float GetScreenRatio()
{
return (1920f / 1080f) / (Screen.height / (float)Screen.width);
}

public static T FindComponentInChildWithTag<T>(this GameObject parent, string tag) where T : Component
{
Transform t = parent.transform;
foreach (Transform tr in t)
{
if (tr.tag == tag)
{
return tr.GetComponent<T>();
}
}

return null;
}

public static void SetLayerForAllChildObject(GameObject obj, int layerIndex)
{
obj.layer = layerIndex;
obj.GetComponentsInChildren<Transform>().ToList().ForEach(x => { x.gameObject.layer = layerIndex; });
}

public static void SetTagForAllChildObject(GameObject obj, string tag)
{
obj.tag = tag;
obj.GetComponentsInChildren<Transform>().ToList().ForEach(x => { x.gameObject.tag = tag; });
}
}
}
96 changes: 96 additions & 0 deletions VirtueSky/Misc/GetFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Collections.Generic;
using System.IO;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace VirtueSky.Misc
{
#if UNITY_EDITOR
public static class GetFile
{
public static T GetConfigFromFolder<T>(string path) where T : ScriptableObject
{
var fileEntries = Directory.GetFiles(path, ".", SearchOption.AllDirectories);

foreach (var fileEntry in fileEntries)
if (fileEntry.EndsWith(".asset"))
{
var item =
AssetDatabase.LoadAssetAtPath<T>(fileEntry.Replace("\\", "/"));
if (item)
return item;
}

return null;
}

public static List<T> GetConfigsFromFolder<T>(string path) where T : ScriptableObject
{
var fileEntries = Directory.GetFiles(path, ".", SearchOption.AllDirectories);
var result = new List<T>();
foreach (var fileEntry in fileEntries)
if (fileEntry.EndsWith(".asset"))
{
var item = AssetDatabase.LoadAssetAtPath<T>(fileEntry.Replace("\\", "/"));

if (item)
result.Add(item);
}

if (result.Count > 0)
return result;

return null;
}

public static T GetConfigFromResource<T>(string path) where T : ScriptableObject
{
T config =
Resources.Load<T>(path);
if (config != null)
{
return config;
}

return null;
}

public static T GetPrefabFromFolder<T>(string path) where T : MonoBehaviour
{
var fileEntries = Directory.GetFiles(path, ".", SearchOption.AllDirectories);

foreach (var fileEntry in fileEntries)
if (fileEntry.EndsWith(".prefab"))
{
var item =
AssetDatabase.LoadAssetAtPath<T>(fileEntry.Replace("\\", "/"));
if (item)
return item;
}

return null;
}

public static List<T> GetPrefabsFromFolder<T>(string path) where T : MonoBehaviour
{
var fileEntries = Directory.GetFiles(path, ".", SearchOption.AllDirectories);
var result = new List<T>();
foreach (var fileEntry in fileEntries)
if (fileEntry.EndsWith(".prefab"))
{
var item = AssetDatabase.LoadAssetAtPath<T>(fileEntry.Replace("\\", "/"));

if (item)
result.Add(item);
}

if (result.Count > 0)
return result;

return null;
}
}
#endif
}
3 changes: 3 additions & 0 deletions VirtueSky/Misc/GetFile.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 786026c

Please sign in to comment.