Skip to content

Commit

Permalink
Merge pull request #122 from cs-util-com/feature/ecsImprovements3
Browse files Browse the repository at this point in the history
Feature/ecs improvements3
  • Loading branch information
cs-util authored Feb 8, 2024
2 parents 2b33f29 + 03090dc commit 8634f4f
Show file tree
Hide file tree
Showing 244 changed files with 8,527 additions and 970 deletions.
3 changes: 2 additions & 1 deletion CsCore/CsCoreUnity/Plugins/CsCoreUnity/CsCoreUnity.asmdef
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "CsCoreUnity",
"references": [
"CsCore"
"CsCore",
"Unity.TextMeshPro"
],
"optionalUnityReferences": [],
"includePlatforms": [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
using UnityEngine;
using System.Collections;
using System.Linq;

namespace com.csutil {

public static class AnimationExtensions {

public static AnimationCurve CreateCopyWithModifiedTime(this AnimationCurve source, float timeFactorToMultiply) {
var keys = source.keys.Select(key => {
float newTime = key.time * timeFactorToMultiply;
float newInTangent = key.inTangent / timeFactorToMultiply;
float newOutTangent = key.outTangent / timeFactorToMultiply;
if (key.weightedMode != WeightedMode.None) {
return new Keyframe(newTime, key.value, newInTangent, newOutTangent, key.inWeight, key.outWeight);
} else {
return new Keyframe(newTime, key.value, newInTangent, newOutTangent);
}
}).ToArray();
return new AnimationCurve(keys);
}

public static IEnumerator MoveTo(this Transform self, Transform target, float moveSpeed, AnimationCurve curve) {
var start = self.position;
var waitForEndOfFrame = new WaitForEndOfFrame();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void MoveTargetToNextWaypoint() {
}

private static Transform GetNextChild(Transform parent, int currentIndex, bool loopChildren) {
AssertV2.AreNotEqual(0, parent.childCount);
AssertV3.AreNotEqual(0, parent.childCount);
if (currentIndex + 1 >= parent.childCount) { return loopChildren ? parent.GetChild(0) : null; }
return parent.GetChild(currentIndex + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void MoveTargetToNextWaypoint() {
}

private static Transform GetNextChild(Transform parent, int currentIndex, bool loopChildren) {
AssertV2.AreNotEqual(0, parent.childCount);
AssertV3.AreNotEqual(0, parent.childCount);
if (currentIndex + 1 >= parent.childCount) { return loopChildren ? parent.GetChild(0) : null; }
return parent.GetChild(currentIndex + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static IEnumerator AsCoroutine(this Task self, Action<Exception> onError,
Stopwatch timer = timeoutInMs > 0 ? Stopwatch.StartNew() : null;
while (!self.IsCompleted) {
yield return waitIntervalBeforeNextCheck;
AssertV2.IsTrue(self.Status != TaskStatus.WaitingToRun, "Task is WaitingToRun");
AssertV3.IsTrue(self.Status != TaskStatus.WaitingToRun, () => "Task is WaitingToRun");
if (timer != null && timeoutInMs < timer.ElapsedMilliseconds) {
onError(new TimeoutException("Task timeout after " + timer.ElapsedMilliseconds + "ms"));
break;
Expand Down Expand Up @@ -123,8 +123,10 @@ public static IEnumerator WithErrorCatch(this IEnumerator coroutineToWrap, Actio
try {
if (!coroutineToWrap.MoveNext()) { break; }
current = coroutineToWrap.Current;
} catch (Exception e) {
onRoutineDone(e);
yield break;
}
catch (Exception e) { onRoutineDone(e); yield break; }
yield return current;
}
onRoutineDone(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,35 @@ namespace com.csutil {

public class MainThread : MonoBehaviour {

private static MainThread _instance;

public static MainThread instance {
get {
AssertV2.IsTrue(ApplicationV2.isPlaying, "In EDIT mode!");
return IoC.inject.GetOrAddComponentSingleton<MainThread>(new object());
if (_instance.IsNullOrDestroyed()) {
if (Application.isPlaying) {
_instance = new GameObject("MainThread").AddComponent<MainThread>();
_instance.mainThreadRef = Thread.CurrentThread;
_instance.stopWatch = Stopwatch.StartNew();
DontDestroyOnLoad(_instance.gameObject);
} else {
throw Log.e("MainThread not initialized during playmode via MainThread.instance");
}
}
return _instance;
}
}

/// <summary> Will be false if the app is still in initialization phase and the
/// main thread is not yet ready to use </summary>
public static bool IsReadyToUse => mainThreadRef != null;

public static bool isMainThread { get { return mainThreadRef.Equals(Thread.CurrentThread); } }
public static bool IsReadyToUse => _instance != null;

private static Thread mainThreadRef;
public static bool isMainThread => IsReadyToUse && instance.mainThreadRef.Equals(Thread.CurrentThread);

private Thread mainThreadRef;

public long maxAllowedTaskDurationInMsPerFrame = 33;
private Stopwatch stopWatch;
private bool WasInitializedWhilePlaying { get { return stopWatch != null; } }
private ConcurrentQueue<Action> actionsForMainThread = new ConcurrentQueue<Action>();

private void Awake() {
if (mainThreadRef != null) { throw Log.e("There is already a MainThread"); }
mainThreadRef = Thread.CurrentThread;
}

private void OnEnable() {
if (mainThreadRef != Thread.CurrentThread) { mainThreadRef = Thread.CurrentThread; }
stopWatch = Stopwatch.StartNew();
}

private void OnDestroy() {
mainThreadRef = null;
}
private readonly ConcurrentQueue<Action> actionsForMainThread = new ConcurrentQueue<Action>();

private void Update() {
if (!actionsForMainThread.IsEmpty) {
Expand Down Expand Up @@ -79,15 +75,7 @@ public static Task Invoke<T>(Func<Task> a) {
}

public void ExecuteOnMainThread(Action a) {
if (ApplicationV2.isPlaying) { AssertV2.IsNotNull(mainThreadRef, "mainThreadRef"); }
if (WasInitializedWhilePlaying) {
actionsForMainThread.Enqueue(a);
} else if (!ApplicationV2.isPlaying) {
Log.d("ExecuteOnMainThread: Application not playing, action will be instantly executed now");
a();
} else {
throw Log.e("MainThread not initialized via MainThread.instance");
}
actionsForMainThread.Enqueue(a);
}

[Obsolete("It's recommended to use ExecuteOnMainThreadAsync instead")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override Task RunTask(Action action) {
}

private static Task StartCoroutineAsTask(IEnumerator iEnum) {
AssertV2.IsTrue(ApplicationV2.isPlaying, "In EDIT mode!");
AssertV3.IsTrue(ApplicationV2.isPlaying, () => "In EDIT mode!");
var tcs = new TaskCompletionSource<bool>();
MainThread.Invoke(() => { MainThread.instance.StartCoroutineAsTask(tcs, iEnum, () => true); });
return tcs.Task;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using UnityEditor;
using UnityEngine;

namespace com.csutil.editor {

/// <summary> Modified version of https://github.com/sirgru/Unity-Simple-Editor-Shortcuts-Tools-Collection </summary>
public static class CopyPasteTransformComponent {

private static TransformData copiedTransformValues;

[MenuItem("Edit/Copy Transform Values %#c", false, -101)]
public static void CopyTransformValues() {
if (Selection.gameObjects.Length == 0) return;
var transformToCopy = Selection.gameObjects[0].transform;

if (transformToCopy is RectTransform rt) {
copiedTransformValues = new TransformData() {
position = transformToCopy.position,
rotation = transformToCopy.rotation,
lossyScale = transformToCopy.lossyScale,
};
// Additionally copy the anchors, pivot, width and height etc:
copiedTransformValues.anchorMin = rt.anchorMin;
copiedTransformValues.anchorMax = rt.anchorMax;
copiedTransformValues.anchoredPosition = rt.anchoredPosition;
copiedTransformValues.pivot = rt.pivot;
copiedTransformValues.sizeDelta = rt.sizeDelta;
} else {
copiedTransformValues = new TransformData() {
position = transformToCopy.position,
rotation = transformToCopy.rotation,
lossyScale = transformToCopy.lossyScale
};
}
}

[MenuItem("Edit/Paste Transform Values %#v", false, -101)]
public static void PasteTransformValues() {
foreach (var selection in Selection.gameObjects) {
Transform targetTransform = selection.transform;
Undo.RecordObject(targetTransform, "Paste Transform Values");

targetTransform.position = copiedTransformValues.position;
targetTransform.rotation = copiedTransformValues.rotation;
targetTransform.scale(copiedTransformValues.lossyScale);
if (targetTransform is RectTransform rt) {
rt.anchorMin = copiedTransformValues.anchorMin;
rt.anchorMax = copiedTransformValues.anchorMax;
rt.anchoredPosition = copiedTransformValues.anchoredPosition;
rt.pivot = copiedTransformValues.pivot;
rt.sizeDelta = copiedTransformValues.sizeDelta;
}

}
}

private struct TransformData {
public Vector3 position;
public Quaternion rotation;
public Vector3 lossyScale;
public Vector2 anchorMin;
public Vector2 anchorMax;
public Vector2 anchoredPosition;
public Vector2 pivot;
public Vector2 sizeDelta;
}

}

}

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

Loading

0 comments on commit 8634f4f

Please sign in to comment.