support hot-reload.
This commit is contained in:
@@ -1,18 +1,22 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(uDesktopDuplication.Texture)),
|
||||
RequireComponent(typeof(uDesktopDuplication.Cursor))]
|
||||
public class Loupe : MonoBehaviour
|
||||
{
|
||||
private uDesktopDuplication.Texture uddTexture_;
|
||||
private uDesktopDuplication.Cursor uddCursor_;
|
||||
public float zoom = 3f;
|
||||
public float aspect = 1f;
|
||||
|
||||
void Start()
|
||||
{
|
||||
uddTexture_ = GetComponent<uDesktopDuplication.Texture>();
|
||||
uddCursor_ = GetComponent<uDesktopDuplication.Cursor>();
|
||||
uddTexture_.useClip = true;
|
||||
}
|
||||
|
||||
void Update()
|
||||
void LateUpdate()
|
||||
{
|
||||
CheckVariables();
|
||||
|
||||
@@ -26,11 +30,12 @@ public class Loupe : MonoBehaviour
|
||||
}
|
||||
|
||||
var monitor = uddTexture_.monitor;
|
||||
var cursorX = monitor.isCursorVisible ? monitor.cursorX : monitor.systemCursorX;
|
||||
var cursorY = monitor.isCursorVisible ? monitor.cursorY : monitor.systemCursorY;
|
||||
|
||||
var x = (float)cursorX / monitor.width;
|
||||
var y = (float)cursorY / monitor.height;
|
||||
var x = monitor.isCursorVisible ?
|
||||
uddCursor_.coord.x :
|
||||
(float)monitor.systemCursorX / monitor.width;
|
||||
var y = monitor.isCursorVisible ?
|
||||
uddCursor_.coord.y :
|
||||
(float)monitor.systemCursorY / monitor.height;
|
||||
var w = 1f / zoom;
|
||||
var h = w / aspect * monitor.aspect;
|
||||
x = Mathf.Clamp(x - w / 2, 0f, 1f - w);
|
||||
@@ -40,7 +45,7 @@ public class Loupe : MonoBehaviour
|
||||
}
|
||||
|
||||
void CheckVariables()
|
||||
{
|
||||
{
|
||||
if (zoom < 1f) zoom = 1f;
|
||||
if (aspect < 0.01f) aspect = 0.01f;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using UnityEngine.Assertions;
|
||||
using System.Collections.Generic;
|
||||
using MeshForwardDirection = uDesktopDuplication.Texture.MeshForwardDirection;
|
||||
using MonitorState = uDesktopDuplication.MonitorState;
|
||||
|
||||
public class MultipleMonitorCreator : MonoBehaviour
|
||||
{
|
||||
@@ -36,12 +37,6 @@ public class MultipleMonitorCreator : MonoBehaviour
|
||||
bool hasMonitorUnsupportStateChecked_ = false;
|
||||
float removeWaitTimer_ = 0f;
|
||||
|
||||
public class SavedMonitorInfo
|
||||
{
|
||||
public float widthScale = 1f;
|
||||
public float heightScale = 1f;
|
||||
}
|
||||
|
||||
public class MonitorInfo
|
||||
{
|
||||
public GameObject gameObject { get; set; }
|
||||
@@ -54,6 +49,12 @@ public class MultipleMonitorCreator : MonoBehaviour
|
||||
private List<MonitorInfo> monitors_ = new List<MonitorInfo>();
|
||||
public List<MonitorInfo> monitors { get { return monitors_; } }
|
||||
|
||||
public class SavedMonitorInfo
|
||||
{
|
||||
public float widthScale = 1f;
|
||||
public float heightScale = 1f;
|
||||
}
|
||||
|
||||
private List<SavedMonitorInfo> savedInfoList_ = new List<SavedMonitorInfo>();
|
||||
public List<SavedMonitorInfo> savedInfoList { get { return savedInfoList_; } }
|
||||
|
||||
@@ -90,12 +91,12 @@ public class MultipleMonitorCreator : MonoBehaviour
|
||||
removeWaitTimer_ += Time.deltaTime;
|
||||
if (removeWaitTimer_ > removeWaitDuration) {
|
||||
hasMonitorUnsupportStateChecked_ = true;
|
||||
foreach (var info in monitors_) {
|
||||
if (info.uddTexture.monitor.state == uDesktopDuplication.MonitorState.Unsupported) {
|
||||
foreach (var info in monitors) {
|
||||
if (info.uddTexture.monitor.state == MonitorState.Unsupported) {
|
||||
Destroy(info.gameObject);
|
||||
}
|
||||
}
|
||||
monitors_.RemoveAll(info => info.uddTexture.monitor.state == uDesktopDuplication.MonitorState.Unsupported);
|
||||
monitors.RemoveAll(info => info.uddTexture.monitor.state == MonitorState.Unsupported);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,14 +112,16 @@ public class MultipleMonitorCreator : MonoBehaviour
|
||||
ResetRemoveTimer();
|
||||
|
||||
// Create monitors
|
||||
for (int i = 0; i < uDesktopDuplication.Manager.monitors.Count; ++i) {
|
||||
var n = uDesktopDuplication.Manager.monitors.Count;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
// Create monitor obeject
|
||||
var go = Instantiate(monitorPrefab);
|
||||
go.name = "Monitor " + i;
|
||||
|
||||
// Saved infomation
|
||||
if (savedInfoList.Count == i) {
|
||||
savedInfoList.Add(new SavedMonitorInfo());
|
||||
savedInfoList.Add(new SavedMonitorInfo());
|
||||
Assert.AreEqual(i, savedInfoList.Count - 1);
|
||||
}
|
||||
var savedInfo = savedInfoList[i];
|
||||
|
||||
@@ -164,24 +167,22 @@ public class MultipleMonitorCreator : MonoBehaviour
|
||||
go.transform.SetParent(transform);
|
||||
|
||||
// Save
|
||||
{
|
||||
var info = new MonitorInfo();
|
||||
info.gameObject = go;
|
||||
info.originalRotation = go.transform.rotation;
|
||||
info.originalLocalScale = go.transform.localScale;
|
||||
info.uddTexture = texture;
|
||||
info.mesh = mesh;
|
||||
monitors.Add(info);
|
||||
}
|
||||
var info = new MonitorInfo();
|
||||
info.gameObject = go;
|
||||
info.originalRotation = go.transform.rotation;
|
||||
info.originalLocalScale = go.transform.localScale;
|
||||
info.uddTexture = texture;
|
||||
info.mesh = mesh;
|
||||
monitors.Add(info);
|
||||
}
|
||||
|
||||
// Sort monitors in coordinate order
|
||||
monitors_.Sort((a, b) => a.uddTexture.monitor.left - b.uddTexture.monitor.left);
|
||||
monitors.Sort((a, b) => a.uddTexture.monitor.left - b.uddTexture.monitor.left);
|
||||
}
|
||||
|
||||
void Clear()
|
||||
{
|
||||
foreach (var info in monitors_) {
|
||||
foreach (var info in monitors) {
|
||||
Destroy(info.gameObject);
|
||||
}
|
||||
if (removeChildrenWhenClear) {
|
||||
@@ -189,7 +190,7 @@ public class MultipleMonitorCreator : MonoBehaviour
|
||||
Destroy(transform.GetChild(i).gameObject);
|
||||
}
|
||||
}
|
||||
monitors_.Clear();
|
||||
monitors.Clear();
|
||||
}
|
||||
|
||||
[ContextMenu("Recreate")]
|
||||
|
||||
@@ -20,7 +20,7 @@ public class MultipleMonitorRoundLayouter : MultipleMonitorLayouter
|
||||
|
||||
// Keep the local scale z of monitors as 1 to bend them correctly.
|
||||
// And save width / height to use same values after reinitialization.
|
||||
for (int i = 0; i < monitors.Count; ++i) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
var info = monitors[i];
|
||||
var savedInfo = creator_.savedInfoList[i];
|
||||
var scale = info.gameObject.transform.localScale;
|
||||
|
||||
@@ -10,7 +10,8 @@ public class Cursor : MonoBehaviour
|
||||
{
|
||||
[SerializeField] Vector2 modelScale = Vector2.one;
|
||||
|
||||
Vector3 worldPosition { get; set; }
|
||||
public Vector3 worldPosition { get; set; }
|
||||
public Vector2 coord { get; set; }
|
||||
|
||||
private Texture uddTexture_;
|
||||
private Monitor monitor { get { return uddTexture_.monitor; } }
|
||||
@@ -65,6 +66,8 @@ public class Cursor : MonoBehaviour
|
||||
var w = (float)monitor.cursorShapeWidth / monitor.width;
|
||||
var h = (float)monitor.cursorShapeHeight / monitor.height;
|
||||
uddTexture_.material.SetVector("_CursorPositionScale", new Vector4(x, y, w, h));
|
||||
|
||||
coord = new Vector2(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@ public class Manager : MonoBehaviour
|
||||
private Coroutine renderCoroutine_ = null;
|
||||
private bool shouldReinitialize_ = false;
|
||||
private float reinitializationTimer_ = 0f;
|
||||
private bool isFirstFrame_ = true;
|
||||
|
||||
public delegate void ReinitializeHandler();
|
||||
public static event ReinitializeHandler onReinitialized;
|
||||
@@ -72,16 +73,17 @@ public class Manager : MonoBehaviour
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Lib.SetDebugMode(debugMode);
|
||||
Lib.SetTimeout(desktopDuplicationApiTimeout);
|
||||
Lib.InitializeUDD();
|
||||
|
||||
// for simple singleton
|
||||
if (instance_ != null) {
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
instance_ = this;
|
||||
|
||||
Lib.SetDebugMode(debugMode);
|
||||
Lib.SetTimeout(desktopDuplicationApiTimeout);
|
||||
Lib.InitializeUDD();
|
||||
|
||||
CreateMonitors();
|
||||
}
|
||||
|
||||
@@ -94,6 +96,9 @@ public class Manager : MonoBehaviour
|
||||
void OnEnable()
|
||||
{
|
||||
renderCoroutine_ = StartCoroutine(OnRender());
|
||||
if (!isFirstFrame_) {
|
||||
Reinitialize();
|
||||
}
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
@@ -109,6 +114,7 @@ public class Manager : MonoBehaviour
|
||||
Lib.Update();
|
||||
ReinitializeIfNeeded();
|
||||
UpdateMessage();
|
||||
isFirstFrame_ = false;
|
||||
}
|
||||
|
||||
[ContextMenu("Reinitialize")]
|
||||
|
||||
@@ -21,6 +21,7 @@ public class Texture : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private int lastMonitorId_ = 0;
|
||||
public int monitorId
|
||||
{
|
||||
get { return monitor.id; }
|
||||
@@ -125,10 +126,20 @@ public class Texture : MonoBehaviour
|
||||
|
||||
void Update()
|
||||
{
|
||||
KeepMonitor();
|
||||
monitor.shouldBeUpdated = true;
|
||||
UpdateMaterial();
|
||||
}
|
||||
|
||||
void KeepMonitor()
|
||||
{
|
||||
if (monitor == null) {
|
||||
Reinitialize();
|
||||
} else {
|
||||
lastMonitorId_ = monitorId;
|
||||
}
|
||||
}
|
||||
|
||||
void AddCursorIfNotAttached()
|
||||
{
|
||||
if (!GetComponent<Cursor>())
|
||||
@@ -140,7 +151,7 @@ public class Texture : MonoBehaviour
|
||||
void Reinitialize()
|
||||
{
|
||||
// Monitor instance is released here when initialized.
|
||||
monitor = Manager.GetMonitor(monitor.id);
|
||||
monitor = Manager.GetMonitor(lastMonitorId_);
|
||||
}
|
||||
|
||||
void UpdateMaterial()
|
||||
|
||||
Reference in New Issue
Block a user