找回密码
 立即注册
查看: 212|回复: 0

Unity C# ScriptableObject

[复制链接]
发表于 2022-12-4 19:45 | 显示全部楼层 |阅读模式
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

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

[CreateAssetMenu(fileName = "DataItemList_SO", menuName = "Inventory/DataItemList")]

public class DataItemList_SO : ScriptableObject

{

  [SerializeField]//因为可将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;

[System.Serializable]

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;

  [Range(0,1)]

  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

[RequireComponent(typeof(SpriteRenderer))]//没有这个组件时,可自动添加

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插件的函数

  }

}
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-15 20:03 , Processed in 0.089660 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表