fwalker 发表于 2022-12-4 19:45

Unity C# ScriptableObject

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

//在Asset菜单下添加选项,用于生成Script



public class DataItemList_SO : ScriptableObject

{

//因为可将private的参数在inspect串口编辑

private List<ItemDetails> ItemDetails;//可以将ItemDetails传入列表中,由于声明为SerializeField,可在inspect串口中编辑ItemDetails中的数据

}

public enum ItemType//声明枚举类

{

Seed,Commodity,Furiture,

HoeTool,ChopTool,BreakTool,ReapTool,WatterTool,Collectool,

ReapableScenery

}

//setting类专门在unity中保存数据

public class settings

{

public const float fadeduration=0.35f;

public const float fadeAlpha=0.45f;

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;



public class ItemDetails//用ItemDetails类来保存物体数据

{

public int itemID;

public string name;

public Sprite itemicon;

public Sprite itemonworldicon;

public string itemdescription;

public ItemType ItemType;

public int itemuseradius;

public bool canpicked;

public bool candropped;

public bool cancarried;

public int itemprice;



public float sellpercentage;

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class TriggerItemFigure : MonoBehaviour

{

//碰撞事件,碰撞体进入

private void OnTriggerEnter2D(Collider2D collision)

{

    FadeItem[] fadeItems = collision.GetComponentsInChildren<FadeItem>();

    if (fadeItems.Length >= 1) {

      foreach (var item in fadeItems) {

      item.FadeOut();   

      }

    }

}

//碰撞事件,碰撞体离开

private void OnTriggerExit2D(Collider2D collision)

{

    FadeItem[] fadeItems = collision.GetComponentsInChildren<FadeItem>();

    if (fadeItems.Length >= 1)

    {

      foreach (var item in fadeItems)

      {

      item.FadeIn();

      }

    }

}

}

sing Cinemachine;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class SwitchBounds : MonoBehaviour

{

// Start is called before the first frame update

void Start()

{

    SwitchBound();

}

// Update is called once per frame

void Update()

{

}

void SwitchBound()

{

    PolygonCollider2D bound = GameObject.FindGameObjectWithTag("Bound").GetComponent<PolygonCollider2D>();//获取碰撞体

    CinemachineConfiner confiner = GetComponent<CinemachineConfiner>();//获取confiner

    confiner.m_BoundingShape2D = bound;//在inspect串口点击问号,可找到m_BoundingShape2D就是需要赋值的碰撞体

    confiner.InvalidatePathCache();//清除缓存

}

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using DG.Tweening;//在资源商店下载dotween

//没有这个组件时,可自动添加

public class FadeItem : MonoBehaviour

{

private SpriteRenderer sprite;

// Start is calld before the first frame update

void Start()

{

    sprite = GetComponent<SpriteRenderer>();//获取图片渲染组件

}

// Update is called once per frame

void Update()

{

}

/// <summary>

/// 逐渐恢复颜色

/// </summary>

public void FadeIn()

{

    Color targetcolor = new Color(1,1,1,1);//透明度为1

    sprite.DOColor(targetcolor,settings.fadeduration);//使用了dotween插件的函数

}

/// <summary>

/// 逐渐半透明

/// </summary>

public void FadeOut()

{

    Color targetcolor = new Color(1, 1, 1, settings.fadeAlpha);//第四个参数为透明度

    sprite.DOColor(targetcolor, settings.fadeduration);//使用了dotween插件的函数

}

}
页: [1]
查看完整版本: Unity C# ScriptableObject