Unity Roguelite 原型 #1
图 1:走廊与房间预制体
- 以 ScriptableObject 定义道具与敌人参数;
- 使用 Addressables 动态加载房间与怪物;
- 基于 URP 自定义光照与描边。
数据结构(代码组)
Section titled “数据结构(代码组)”using UnityEngine;
[CreateAssetMenu(menuName="Defs/Item")]public class ItemDef : ScriptableObject{ public string Id; public string DisplayName; public Sprite Icon; // [!code highlight] [TextArea] public string Desc;// [!code highlight] public int Rarity;}using UnityEngine;using UnityEngine.AddressableAssets;
public class Spawner : MonoBehaviour{ public AssetReferenceGameObject EnemyRef;
async void Start() { var handle = EnemyRef.InstantiateAsync(transform.position, Quaternion.identity); await handle.Task; // [!code highlight] }}Shader 片段(URP/ShaderLab)
Section titled “Shader 片段(URP/ShaderLab)”float4 OutlineFrag (v2f i) : SV_Target{ float3 col = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv).rgb; float edge = 1.0 - step(0.2, length(i.normalWS)); // [!code warning] return float4(lerp(col, float3(0,0,0), edge * _OutlineStrength), 1);}引用:Addressables 避免场景冗大构建;ScriptableObject 让配置与逻辑解耦。
重构(Diff)
Section titled “重构(Diff)”await handle.Task;try { await handle.Task; // [!code highlight]} catch (System.Exception ex) { Debug.LogError($"Spawn failed: {ex.Message}"); // [!code error]}常见问题
- Addressables Group 未标记可远程加载;
- URP 渲染队列/深度写入设置不当导致描边穿透;
终端(CI 构建占位)
Section titled “终端(CI 构建占位)”Unity.exe -batchmode -quit -projectPath . -buildTarget Win64 -executeMethod Build.Perform展开:掉落表与权重
使用加权随机(alias method)~O(1) 采样,将稀有度映射到概率区间。
| 系统 | 进度 | 备注 |
|---|---|---|
| 房间生成 | ✅ | 规则+模板 |
| 敌人池 | 🚧 | 待补远程表 |
| 装备 | ⏸️ | 等待美术 |