From bebe8644ef7e04f8f24da176e4ccc679200849bb Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 27 Dec 2016 00:34:51 +0700 Subject: [PATCH] fix scene view sorting --- .../Scripts/Simple/CubeAutoMovement.cs | 14 ++++- .../Scripts/Internal/IsoScreenSolver.cs | 60 +++++++++++++++---- .../Scripts/Internal/IsoSortingSolver.cs | 4 ++ Assets/IsoTools/Scripts/IsoWorld.cs | 12 ++-- 4 files changed, 72 insertions(+), 18 deletions(-) diff --git a/Assets/IsoTools/Examples/Scripts/Simple/CubeAutoMovement.cs b/Assets/IsoTools/Examples/Scripts/Simple/CubeAutoMovement.cs index 0d8e401..02843d7 100644 --- a/Assets/IsoTools/Examples/Scripts/Simple/CubeAutoMovement.cs +++ b/Assets/IsoTools/Examples/Scripts/Simple/CubeAutoMovement.cs @@ -7,14 +7,22 @@ namespace IsoTools.Examples.Simple { public float stepTicks = 0.5f; public float stepRndTicks = 0.5f; - IsoObject _isoObject = null; + Vector3 _startPos = Vector3.zero; + IsoObject _isoObject = null; + Coroutine _movementCoro = null; - void Start() { + void OnEnable() { _isoObject = GetComponent(); if ( !_isoObject ) { throw new UnityException("CubeAutoMovement. IsoObject component not found!"); } - StartCoroutine(Move()); + _startPos = _isoObject.position; + _movementCoro = StartCoroutine(Move()); + } + + void OnDisable() { + StopCoroutine(_movementCoro); + _isoObject.position = _startPos; } WaitForSeconds RndWait() { diff --git a/Assets/IsoTools/Scripts/Internal/IsoScreenSolver.cs b/Assets/IsoTools/Scripts/Internal/IsoScreenSolver.cs index 1a46794..3338d22 100644 --- a/Assets/IsoTools/Scripts/Internal/IsoScreenSolver.cs +++ b/Assets/IsoTools/Scripts/Internal/IsoScreenSolver.cs @@ -1,6 +1,10 @@ using UnityEngine; using System.Collections.Generic; +#if UNITY_EDITOR +using UnityEditor; +#endif + #if UNITY_5_5_OR_NEWER using UnityEngine.Profiling; #endif @@ -8,6 +12,7 @@ using UnityEngine.Profiling; namespace IsoTools.Internal { public class IsoScreenSolver { Vector2 _minIsoXY = Vector2.zero; + IsoAssocList _oldVisibles = new IsoAssocList(); IsoAssocList _curVisibles = new IsoAssocList(); @@ -47,22 +52,55 @@ namespace IsoTools.Internal { Camera[] _tmpCameras = new Camera[8]; IsoScreenSolver _screenSolver = null; + // + // Public + // + public void LookUp(IsoObject iso_object) { iso_object.Internal.Placed = false; _screenSolver._oldVisibles.Add(iso_object); } - public void LookUpForVisibility(IsoScreenSolver screen_solver) { + public void LookUpForVisibility(IsoScreenSolver screen_solver, bool include_scene_view) { _screenSolver = screen_solver; - var cam_count = Camera.GetAllCameras(_tmpCameras); + var cam_count = FillLookUpCameras(include_scene_view); for ( var i = 0; i < cam_count; ++i ) { - var camera = _tmpCameras[i]; - var vp_rect = camera.rect; - var wrl_min = camera.ViewportToWorldPoint(new Vector3(vp_rect.xMin, vp_rect.yMin)); - var wrl_max = camera.ViewportToWorldPoint(new Vector3(vp_rect.xMax, vp_rect.yMax)); + var tmp_cam = _tmpCameras[i]; + var vp_rect = tmp_cam.rect; + var wrl_min = tmp_cam.ViewportToWorldPoint(new Vector3(vp_rect.xMin, vp_rect.yMin)); + var wrl_max = tmp_cam.ViewportToWorldPoint(new Vector3(vp_rect.xMax, vp_rect.yMax)); _screenSolver._quadTree.VisitItemsByBounds(new IsoRect(wrl_min, wrl_max), this); } + ResetLookUpCameras(); _screenSolver = null; + } + + // + // Private + // + + int FillLookUpCameras(bool include_scene_view) { + var camera_count = Camera.allCamerasCount; + if ( _tmpCameras.Length < camera_count + 1 ) { + _tmpCameras = new Camera[camera_count * 2 + 1]; + } + Camera.GetAllCameras(_tmpCameras); + return include_scene_view + ? AddSceneViewCamera(camera_count) + : camera_count; + } + + int AddSceneViewCamera(int camera_count) { + #if UNITY_EDITOR + var scene_view = SceneView.lastActiveSceneView; + if ( scene_view && scene_view.camera ) { + _tmpCameras[camera_count++] = scene_view.camera; + } + #endif + return camera_count; + } + + void ResetLookUpCameras() { System.Array.Clear(_tmpCameras, 0, _tmpCameras.Length); } } @@ -201,7 +239,7 @@ namespace IsoTools.Internal { public void StepSortingAction(IsoWorld iso_world, IsoAssocList instances) { Profiler.BeginSample("IsoScreenSolver.ResolveVisibles"); - ResolveVisibles(instances); + ResolveVisibles(iso_world, instances); Profiler.EndSample(); Profiler.BeginSample("IsoScreenSolver.ResolveVisibleGrid"); ResolveVisibleGrid(iso_world); @@ -241,12 +279,12 @@ namespace IsoTools.Internal { // // --------------------------------------------------------------------- - void ResolveVisibles(IsoAssocList instances) { + void ResolveVisibles(IsoWorld iso_world, IsoAssocList instances) { Profiler.BeginSample("ProcessAllInstances"); ProcessAllInstances(instances); Profiler.EndSample(); Profiler.BeginSample("ProcessNewVisibles"); - ProcessNewVisibles(); + ProcessNewVisibles(iso_world.isSortInSceneView); Profiler.EndSample(); } @@ -264,9 +302,9 @@ namespace IsoTools.Internal { } } - void ProcessNewVisibles() { + void ProcessNewVisibles(bool include_scene_view) { _oldVisibles.Clear(); - _qtContentLU.LookUpForVisibility(this); + _qtContentLU.LookUpForVisibility(this, include_scene_view); SwapCurrentVisibles(); } diff --git a/Assets/IsoTools/Scripts/Internal/IsoSortingSolver.cs b/Assets/IsoTools/Scripts/Internal/IsoSortingSolver.cs index 3713233..ab47d4d 100644 --- a/Assets/IsoTools/Scripts/Internal/IsoSortingSolver.cs +++ b/Assets/IsoTools/Scripts/Internal/IsoSortingSolver.cs @@ -1,6 +1,10 @@ using UnityEngine; using System.Collections.Generic; +#if UNITY_EDITOR +using UnityEditor; +#endif + #if UNITY_5_5_OR_NEWER using UnityEngine.Profiling; #endif diff --git a/Assets/IsoTools/Scripts/IsoWorld.cs b/Assets/IsoTools/Scripts/IsoWorld.cs index 98c481e..d5a0cd6 100644 --- a/Assets/IsoTools/Scripts/IsoWorld.cs +++ b/Assets/IsoTools/Scripts/IsoWorld.cs @@ -307,6 +307,11 @@ namespace IsoTools { get { return _snapByObjects; } set { _snapByObjects = value; } } + [SerializeField] bool _sortInSceneView = true; + public bool isSortInSceneView { + get { return _sortInSceneView; } + set { _sortInSceneView = value; } + } [Header("Development Only")] [SerializeField] bool _showDepends = false; public bool isShowDepends { @@ -401,8 +406,6 @@ namespace IsoTools { protected override void OnEnable() { base.OnEnable(); - _screenSolver.Clear(); - _sortingSolver.Clear(); } protected override void OnDisable() { @@ -443,8 +446,9 @@ namespace IsoTools { } void OnRenderObject() { - if ( Camera.current && Camera.current.name == "SceneCamera" ) { - StepSortingProcess(); + var camera = Camera.current; + if ( camera && camera.name == "SceneCamera" ) { + MarkDirty(); } }