1
This commit is contained in:
17
Unity/Scripts/WorldChunk/BITKit.WorkdChunk.asmdef
Normal file
17
Unity/Scripts/WorldChunk/BITKit.WorkdChunk.asmdef
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "BITKit.WorkdChunk",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:a209c53514018594f9f482516f2a6781",
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
25
Unity/Scripts/WorldChunk/IWorldBound.cs
Normal file
25
Unity/Scripts/WorldChunk/IWorldBound.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit.WorldChunk
|
||||
{
|
||||
public interface IWorldChunk
|
||||
{
|
||||
Rect GetRect();
|
||||
void SetActive(bool active);
|
||||
}
|
||||
public abstract class WorldChunk : MonoBehaviour, IWorldChunk
|
||||
{
|
||||
public static readonly List<IWorldChunk> chunks=new();
|
||||
public abstract Rect GetRect();
|
||||
public abstract void SetActive(bool active);
|
||||
void OnEnable()
|
||||
{
|
||||
chunks.Add(this);
|
||||
}
|
||||
void OnDisable()
|
||||
{
|
||||
chunks.Remove(this);
|
||||
}
|
||||
}
|
||||
}
|
39
Unity/Scripts/WorldChunk/ObjectChunk.cs
Normal file
39
Unity/Scripts/WorldChunk/ObjectChunk.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
namespace BITKit.WorldChunk
|
||||
{
|
||||
public class ObjectChunk : WorldChunk
|
||||
{
|
||||
public Vector2 size;
|
||||
public Renderer[] renderers;
|
||||
Vector2 posCenter
|
||||
{
|
||||
get
|
||||
{
|
||||
var pos = transform.position;
|
||||
return new()
|
||||
{
|
||||
x = pos.x,
|
||||
y = pos.z,
|
||||
};
|
||||
}
|
||||
}
|
||||
public override Rect GetRect()
|
||||
{
|
||||
return new()
|
||||
{
|
||||
position = posCenter,
|
||||
size = size,
|
||||
};
|
||||
}
|
||||
public override void SetActive(bool active)
|
||||
{
|
||||
renderers.ForEach(x =>
|
||||
{
|
||||
x.enabled = active;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
20
Unity/Scripts/WorldChunk/TerrainChunk.cs
Normal file
20
Unity/Scripts/WorldChunk/TerrainChunk.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit.WorldChunk
|
||||
{
|
||||
|
||||
public class TerrainChunk : WorldChunk
|
||||
{
|
||||
public Terrain terrain;
|
||||
public override Rect GetRect()
|
||||
{
|
||||
Vector3 size = terrain.terrainData.size;
|
||||
return new(transform.position, size);
|
||||
}
|
||||
public override void SetActive(bool active)
|
||||
{
|
||||
terrain.enabled = active;
|
||||
}
|
||||
}
|
||||
}
|
32
Unity/Scripts/WorldChunk/WorldChunkManager.cs
Normal file
32
Unity/Scripts/WorldChunk/WorldChunkManager.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
namespace BITKit.WorldChunk
|
||||
{
|
||||
public class WorldChunkManager : MonoBehaviour
|
||||
{
|
||||
public Vector2Int playerSize;
|
||||
Rect playerRect;
|
||||
void FixedUpdate()
|
||||
{
|
||||
var cameraPos = Camera.main.transform.position;
|
||||
Vector2 position = new()
|
||||
{
|
||||
x = cameraPos.x - playerSize.x / 2,
|
||||
y = cameraPos.z - playerSize.y / 2,
|
||||
};
|
||||
playerRect = new(position, playerSize);
|
||||
OnUpdate();
|
||||
//ThreadHelper.Add(OnUpdate);
|
||||
}
|
||||
void OnUpdate()
|
||||
{
|
||||
WorldChunk.chunks.ForEach(x =>
|
||||
{
|
||||
var isOverlay = playerRect.Overlaps(x.GetRect());
|
||||
x.SetActive(isOverlay);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user