mirror of
https://github.com/BlackMATov/unity-iso-tools.git
synced 2025-12-16 22:16:55 +07:00
RC folder structure and images
This commit is contained in:
140
Assets/IsoTools/Scripts/IsoObject.cs
Normal file
140
Assets/IsoTools/Scripts/IsoObject.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace IsoTools {
|
||||
[ExecuteInEditMode]
|
||||
public class IsoObject : MonoBehaviour {
|
||||
|
||||
Transform _transform = null;
|
||||
Vector3 _lastPosition = Vector3.zero;
|
||||
Vector3 _lastTransform = Vector3.zero;
|
||||
|
||||
[SerializeField]
|
||||
Vector3 _position = Vector3.zero;
|
||||
public Vector3 Position {
|
||||
get { return _position; }
|
||||
set {
|
||||
_position = value;
|
||||
if ( Alignment ) {
|
||||
FixAlignment();
|
||||
} else {
|
||||
FixTransform();
|
||||
}
|
||||
MartDirtyIsoWorld();
|
||||
if ( Application.isEditor ) {
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
Vector3 _size = Vector3.one;
|
||||
public Vector3 Size {
|
||||
get { return _size; }
|
||||
set {
|
||||
_size = value;
|
||||
if ( Alignment ) {
|
||||
FixAlignment();
|
||||
} else {
|
||||
FixTransform();
|
||||
}
|
||||
MartDirtyIsoWorld();
|
||||
if ( Application.isEditor ) {
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
bool _alignment = true;
|
||||
public bool Alignment {
|
||||
get { return _alignment; }
|
||||
set {
|
||||
_alignment = value;
|
||||
if ( Alignment ) {
|
||||
FixAlignment();
|
||||
} else {
|
||||
FixTransform();
|
||||
}
|
||||
MartDirtyIsoWorld();
|
||||
if ( Application.isEditor ) {
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IsoWorld _iso_world = null;
|
||||
public IsoWorld GetIsoWorld() {
|
||||
if ( !_iso_world ) {
|
||||
_iso_world = GameObject.FindObjectOfType<IsoWorld>();
|
||||
}
|
||||
if ( !_iso_world ) {
|
||||
throw new UnityException("IsoObject. IsoWorld not found!");
|
||||
}
|
||||
return _iso_world;
|
||||
}
|
||||
|
||||
public void ResetIsoWorld() {
|
||||
_iso_world = null;
|
||||
}
|
||||
|
||||
public void FixAlignment() {
|
||||
_position.Set(
|
||||
Mathf.Round(_position.x),
|
||||
Mathf.Round(_position.y),
|
||||
Mathf.Round(_position.z));
|
||||
FixTransform();
|
||||
MartDirtyIsoWorld();
|
||||
if ( Application.isEditor ) {
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void FixTransform() {
|
||||
var iso_world = GetIsoWorld();
|
||||
if ( iso_world && _transform ) {
|
||||
Vector3 trans = iso_world.IsoToScreen(Position);
|
||||
trans.z = _transform.position.z;
|
||||
_transform.position = trans;
|
||||
_lastPosition = Position;
|
||||
_lastTransform = trans;
|
||||
}
|
||||
}
|
||||
|
||||
public void FixIsoPosition() {
|
||||
var iso_world = GetIsoWorld();
|
||||
if ( iso_world && _transform ) {
|
||||
Vector2 trans = _transform.position;
|
||||
Position = iso_world.ScreenToIso(trans, Position.z);
|
||||
FixTransform();
|
||||
}
|
||||
}
|
||||
|
||||
void MartDirtyIsoWorld() {
|
||||
var iso_world = GetIsoWorld();
|
||||
if ( iso_world ) {
|
||||
iso_world.MarkDirty();
|
||||
}
|
||||
}
|
||||
|
||||
void Start() {
|
||||
_transform = gameObject.transform;
|
||||
_lastPosition = Position;
|
||||
_lastTransform = _transform.position;
|
||||
MartDirtyIsoWorld();
|
||||
}
|
||||
|
||||
void Update() {
|
||||
if ( _lastPosition != _position ) {
|
||||
Position = _position;
|
||||
}
|
||||
if ( _lastTransform != _transform.position ) {
|
||||
FixIsoPosition();
|
||||
}
|
||||
}
|
||||
|
||||
void OnEnable() {
|
||||
MartDirtyIsoWorld();
|
||||
}
|
||||
}
|
||||
} // namespace IsoTools
|
||||
8
Assets/IsoTools/Scripts/IsoObject.cs.meta
Normal file
8
Assets/IsoTools/Scripts/IsoObject.cs.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9a9c584f9a39449438abc7ba59a68778
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
155
Assets/IsoTools/Scripts/IsoWorld.cs
Normal file
155
Assets/IsoTools/Scripts/IsoWorld.cs
Normal file
@@ -0,0 +1,155 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IsoTools {
|
||||
[ExecuteInEditMode]
|
||||
public class IsoWorld : MonoBehaviour {
|
||||
|
||||
public float TileSize = 32.0f;
|
||||
public float StartDepth = 0.0f;
|
||||
public float StepDepth = 0.1f;
|
||||
|
||||
class ObjectInfo {
|
||||
public IsoObject IsoObject;
|
||||
public bool Visited;
|
||||
public int BeginDepend;
|
||||
public int EndDepend;
|
||||
public ObjectInfo(IsoObject obj) {
|
||||
IsoObject = obj;
|
||||
}
|
||||
public void Reset(int first_depend) {
|
||||
Visited = false;
|
||||
BeginDepend = first_depend;
|
||||
EndDepend = first_depend;
|
||||
}
|
||||
}
|
||||
|
||||
bool _dirty = true;
|
||||
List<int> _depends = new List<int>();
|
||||
List<ObjectInfo> _objects = new List<ObjectInfo>();
|
||||
float _lastTileSize = 0.0f;
|
||||
|
||||
public void MarkDirty() {
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
public Vector2 IsoToScreen(Vector3 pos) {
|
||||
return new Vector2(
|
||||
(pos.x - pos.y),
|
||||
(pos.x + pos.y) * 0.5f + pos.z) * TileSize;
|
||||
}
|
||||
|
||||
public Vector3 ScreenToIso(Vector2 pos) {
|
||||
return new Vector3(
|
||||
(pos.x * 0.5f + pos.y),
|
||||
(pos.y - pos.x * 0.5f),
|
||||
0.0f) / TileSize;
|
||||
}
|
||||
|
||||
public Vector3 ScreenToIso(Vector2 pos, float iso_z) {
|
||||
var iso_pos = ScreenToIso(new Vector2(pos.x, pos.y - iso_z * TileSize));
|
||||
iso_pos.z = iso_z;
|
||||
return iso_pos;
|
||||
}
|
||||
|
||||
void _fixTileSize() {
|
||||
_scanObjects();
|
||||
foreach ( var obj in _objects ) {
|
||||
obj.IsoObject.FixTransform();
|
||||
}
|
||||
_objects.Clear();
|
||||
_lastTileSize = TileSize;
|
||||
}
|
||||
|
||||
void _fixDirty() {
|
||||
_scanObjects();
|
||||
_scanDepends();
|
||||
_manualSort();
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
void _fixDisable() {
|
||||
_scanObjects();
|
||||
foreach ( var obj in _objects ) {
|
||||
obj.IsoObject.ResetIsoWorld();
|
||||
}
|
||||
_objects.Clear();
|
||||
}
|
||||
|
||||
void _scanObjects() {
|
||||
_objects.Clear();
|
||||
IsoObject[] iso_objects = GameObject.FindObjectsOfType<IsoObject>();
|
||||
foreach ( var iso_object in iso_objects ) {
|
||||
var info = new ObjectInfo(iso_object);
|
||||
_objects.Add(info);
|
||||
}
|
||||
}
|
||||
|
||||
void _scanDepends() {
|
||||
_depends.Clear();
|
||||
foreach ( var obj_a in _objects ) {
|
||||
obj_a.Reset(_depends.Count);
|
||||
var obj_ao = obj_a.IsoObject;
|
||||
var max_ax = obj_ao.Position.x + obj_ao.Size.x;
|
||||
var max_ay = obj_ao.Position.y + obj_ao.Size.y;
|
||||
for ( int i = 0; i < _objects.Count; ++i ) {
|
||||
var obj_bo = _objects[i].IsoObject;
|
||||
if ( obj_ao != obj_bo ) {
|
||||
if ( obj_bo.Position.x < max_ax && obj_bo.Position.y < max_ay ) {
|
||||
var max_bz = obj_bo.Position.z + obj_bo.Size.z;
|
||||
if ( obj_ao.Position.z < max_bz ) {
|
||||
_depends.Add(i);
|
||||
++obj_a.EndDepend;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _manualSort() {
|
||||
var depth = StartDepth;
|
||||
foreach ( ObjectInfo info in _objects ) {
|
||||
_placeObject(info, ref depth);
|
||||
}
|
||||
_objects.Clear();
|
||||
_depends.Clear();
|
||||
}
|
||||
|
||||
void _placeObject(IsoObject obj, float depth) {
|
||||
var pos = obj.gameObject.transform.position;
|
||||
obj.gameObject.transform.position = new Vector3(pos.x, pos.y, depth);
|
||||
}
|
||||
|
||||
void _placeObject(ObjectInfo info, ref float depth) {
|
||||
if ( !info.Visited ) {
|
||||
info.Visited = true;
|
||||
for ( int i = info.BeginDepend; i < info.EndDepend && i < _depends.Count; ++i ) {
|
||||
var object_index = _depends[i];
|
||||
var obj = _objects[object_index];
|
||||
_placeObject(obj, ref depth);
|
||||
}
|
||||
_placeObject(info.IsoObject, depth);
|
||||
depth += StepDepth;
|
||||
}
|
||||
}
|
||||
|
||||
void Start() {
|
||||
_fixTileSize();
|
||||
_fixDirty();
|
||||
}
|
||||
|
||||
void LateUpdate() {
|
||||
if ( _lastTileSize != TileSize ) {
|
||||
_fixTileSize();
|
||||
}
|
||||
if ( _dirty ) {
|
||||
_fixDirty();
|
||||
}
|
||||
}
|
||||
|
||||
void OnDisable() {
|
||||
_fixDisable();
|
||||
}
|
||||
}
|
||||
} // namespace IsoTools
|
||||
8
Assets/IsoTools/Scripts/IsoWorld.cs.meta
Normal file
8
Assets/IsoTools/Scripts/IsoWorld.cs.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f01619d3802e814f86f9e6bb965349a
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
Reference in New Issue
Block a user