Pythonで継承しても親のリストは別物にならない

バージョン

python 3.10.6

概要

プログラム

###
#こんなようなクラスがある
class Base:
    x = 0
    ary1 = [0,0]
    ary2 = []
class Hasei1(Base):
    ary3 = []
class Hasei2(Base):
    ary3 = []

###
#宣言
b = Base()
h1 = Hasei1()
h2 = Hasei2()

###
#変更
h1.x = 3
h1.ary1[0] = 3
h1.ary2.append(3)
h1.ary3.append(30)

###
#表示
print(b.x)     #期待値: 0
print(h1.x)    #期待値: 3
print(h2.x)    #期待値: 0
print(b.ary1)  #期待値: [0,0]
print(h1.ary1) #期待値: [3,0]
print(h2.ary1) #期待値: [0,0]
print(b.ary2)  #期待値: []
print(h1.ary2) #期待値: [3]
print(h2.ary2) #期待値: []
print(h1.ary3) #期待値: [30]
print(h2.ary3) #期待値: []

結果

0
3
0
[3, 0]
[3, 0]
[3, 0]
[3]
[3]
[3]
[30]
[]

なんでやねん

対処

親クラスのコンストラクタかなんかで明示的にリストを代入する必要がある
子クラスで再代入しても別物になる

#たぶん簡単な対処の例
class Base:
    x = 0
    def __init__(self) -> None:
        self.ary1 = [0,0]
        self.ary2 = []
class Hasei1(Base):
    ary3 = []
class Hasei2(Base):
    ary3 = []

これで期待値通りのものがでてくるようになる
詳しい理由は知らないが、新しいインスタンスには既存の値を代入が基本動作なんだろうと適当に片を付けておく

logicool g300sのチャタリングを直す

いるもの

マウス
+-ドライバー
ピンセット

おはなし

マウスの左ボタンがチャタリングしてるので直したろうと思った

1. マウスを分解

裏側の黒いシールのようなちょっと厚めのやつを、-ドライバーとかでペリッと外す
そうするとネジが見えるので外す

2. D2FC-F-7Nのカバーを外す

左ボタンを担当してるのはこれらしい

これを適当に爪とかピンセットとか-ドライバーで、留め爪を外す

すると中身が飛び散るので拾って集める、2つ
参考程度にHB鉛筆を置いたけど、白いのはかなり小さい
(カバーが見にくかったので補正してる)

3. 接触部分の整備をする

で、接触部分を見るとどうやら削れてるっぽい
ここが接触不良をおこしてチャタリングを引き起こしてると決めつける


それをこうじゃ

(接触点を曲げて削れてない部分に当てるようにする)

4. 戻す

これちょっとした豆知識なんですけど
白いのを入れたカバーを裏返してマウスを押し付けると簡単に嵌められるんですね

あとは途中で失くしたりしたネジを全部探して付け直すと元に戻る

経過報告

えっ私のカメラピントぼやけすぎ?!

ちょっとクリック物理音がくぐもった気がするけど、
チャタリング直ったからヨシ!!

ViewからControllerに渡したModelがnullってる

結論

モデルの変数に{get;set;}つけろ

概要

ASP.NET CoreでやるMVCパターンで

//Models
public class HomeModel{
  public string text;
}

//Controllers
public class HomeController : Controller{
  public IActionResult Index(HomeModel m){
    //m == null
  }
}
<!-- Index.cshtml -->
@model Models.HomeModel
<input asp-for="text"/>

な感じでHomeModelに{get;set;}がないと、cshtmlのinputからHomeControllerに値を渡してもnullになる
なんでやねん

public class HomeModel{
  public string text{get;set;}
}

でやると値が入るようになる
詳しいことはわからんがそういうことらしい

Unityで画像入れたら勝手に幅や高さが2の乗数になる

結論

インポートしたあとのTexture Type が Default のままだと
2の乗数になるっぽい(2048とか512とか16とか)

それで、Default から Sprite にすると元の画像サイズになる

もしかしたら、2の乗数じゃなくて4の倍数の可能性もある
4の倍数じゃないと圧縮が効かなさそう
参考
推奨、デフォルト、およびサポートされているテクスチャ形式 (プラットフォーム別) - Unity マニュアル

でもこの辺を参照すると2の乗数かもしれない
テクスチャのインポート - Unity マニュアル

なんにせよ、元の画像サイズにしたいなら Texture Type の変更でどうにかなると思う

あああえ

chatgpt君が出してきたプログラムのデザインパターンのめも
たぶん他にも出してくるだろうけど適当に区切った

Singleton Pattern: A creational pattern that ensures that only one instance of a class is created and provides a global point of access to it.

Factory Method Pattern: A creational pattern that provides an interface for creating objects, but allows subclasses to alter the type of objects that will be created.

Abstract Factory Pattern: A creational pattern that provides an interface for creating families of related objects without specifying their concrete classes.

Builder Pattern: A creational pattern that separates the construction of a complex object from its representation, allowing the same construction process to create different representations.

Adapter Pattern: A structural pattern that allows objects with incompatible interfaces to collaborate by creating a wrapper object that translates one interface to another.

Decorator Pattern: A structural pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.

Facade Pattern: A structural pattern that provides a unified interface to a set of interfaces in a subsystem, making the subsystem easier to use.

Observer Pattern: A behavioral pattern that defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically.

Strategy Pattern: A behavioral pattern that defines a family of algorithms, encapsulates each one, and makes them interchangeable at runtime.

Template Method Pattern: A behavioral pattern that defines the skeleton of an algorithm in a superclass, but lets subclasses override specific steps of the algorithm without changing its structure.

Chain of Responsibility Pattern: A behavioral pattern that allows a chain of objects to handle a request and pass it along the chain until it is handled.

Command Pattern: A behavioral pattern that encapsulates a request as an object, thereby allowing the parameterization of clients with different requests, queue or log requests, and support undoable operations.

Iterator Pattern: A behavioral pattern that provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

State Pattern: A behavioral pattern that allows an object to alter its behavior when its internal state changes, and allows it to appear to change its class.

Visitor Pattern: A behavioral pattern that separates an algorithm from an object structure on which it operates, by moving the hierarchy of methods from the object structure to a separate object.

Proxy Pattern: A structural pattern that provides a surrogate or placeholder for another object to control access to it.

Flyweight Pattern: A structural pattern that reduces the number of objects created by sharing objects that are the same or similar.

Bridge Pattern: A structural pattern that decouples an abstraction from its implementation, so that the two can vary independently.

Composite Pattern: A structural pattern that allows you to compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

Interpreter Pattern: A behavioral pattern that defines a grammatical representation for a language and provides an interpreter to deal with the language.

Mediator Pattern: A behavioral pattern that defines an object that encapsulates how a set of objects interact. It promotes loose coupling by keeping objects from referring to each other explicitly, and it allows their interaction to be varied independently.

Memento Pattern: A behavioral pattern that provides the ability to restore an object to its previous state (undo), or to temporarily save its current state (save point) to roll back to later.

Prototype Pattern: A creational pattern that specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype.

Chain of Command Pattern: A behavioral pattern that lets you pass requests along a chain of handlers, with each handler deciding whether to handle the request or pass it on to the next handler in the chain.

Null Object Pattern: A behavioral pattern that uses an object with a neutral (null) behavior to avoid null pointers and simplify client code.


あとで調べる

あああう

unityに3dモデルとかを入れる度に、ロード用のパス設定がいる
めんどくさい
stringで渡すなんて絶対に打ち間違いする・・・・・・・・・・
__入力候補に表示すればよいのでは?

チュートリアル: 入力候補の表示 - Visual Studio (Windows) | Microsoft Learn
いやもっとめんどいわってなった

まぁなんか適当にフォルダーを回って、
なんちゃら.csに
1.見つかった順にパスをstring[]として羅列する
2.ファイルの名前からenum用の名前にして羅列する
を自動的にやってもらうでいいかってなった
めんどい

あああい

おさらい

さて、使う画像を更新したらunityの方にも自動で反映したいってなった

しかし、外部ファイル読み込みだとロードが遅い、Resourcesは中身が増えると起動が遅くなる、
AssetBundleは旧式化しているのかどうかわからんけどなんかよくわからんので、
ResourcesとAddressablesを使うこととなった
(中身が増えないならResourcesは爆速なので)
たぶんSEやアイコン程度ならResourcesでいいと思う、検証してないけど

けいかく

現状、作成物をAddressablesに入れてプログラム内で使うには次の手順がいる
1. 作成物をunityに入れる
2. 作成物のAddressableを有効にする
3. 作成物のグループ・ラベルを設定する

それ以外にもたくさん設定項目あるんだけど知らんがなって感じでいく

ちょっと調べたら2.3.の自動化は既にある
【Unity】【Addressable】Unity Addressable Importerでアドレスの設定を自動化する - LIGHT11

で、1.だけど
アセットのインポート - Unity マニュアル
アセットデータベースの更新 - Unity マニュアル
この辺りを読む限り直接unityのプロジェクト内のフォルダに入れれば自動的に認識してくれそう
───してくれた

ということで、
画像とかの出力フォルダで更新があったら、更新があったファイルをunity内のフォルダにコピー上書きする
機能を作ればあとはどうにでもなるはず

・・・えっunityフォルダに直接出力すればいいって?
ぐぅの音もでねぇな、でもなんかやだからそうしない
基本的に、絵や音のソフトのファイル出力先がプロジェクトファイルと同じ場所か、前回に指定した場所だったりで、
他のソフトのプロジェクトファイルをunityフォルダ内に入れるのはどうかと思うし、
趣味での出力先とゲームでの出力先が違うし、出力先を選ぶとしても一々フォルダ移動するのが面倒
となると、ゲーム用の出力先フォルダを作ってそこにプロジェクトファイルを入れて、
そのフォルダ内で画像の更新があったらunityに画像だけを持ってくるっていう流れになる
ただ、3Dモデルのようなファイルが複数あって1つの物になってる奴は要考慮
他にいい方法があったら教えてほしい、趣味との併用をやめろは禁止

ぷろぐらむけいかく

1. ファイルパスと最終更新日時のタイムスタンプを一対とする
2. 一対群はcsvかなんかのファイルに保存してあるとする
3. 更新確認のためにファイルから一対群を読み取りメモリに入れる
4. 確認するルートフォルダは1つ、その内部にあるフォルダを全て網羅し、特定拡張子のファイルの更新を監視する
5. 更新があった場合は、UnityのプロジェクトフォルダのAssets下の同じ位置にそのファイルを置く、既にあるなら上書き
6. 該当一対のタイムスタンプを更新しておく
7. 保存ファイルにはあるが該当の場所にない場合は消失フラグを立て、unity内の同じ場所から消す(ないならそのまま)
8. 全部見終わったら消失フラグが立っている以外の一対群をファイルに保存する
9. Unityのソフト起動中に一定間隔で勝手にやってくれるといいなぁ


9.は[InitializeOnLoad]っていう奴があって便利そうだけど
常時起動されてもな・・・っていう思いがあるのでEditorWindowとトグルかなんかで
機能を無効・有効できるようにする
2.はjsonにしようかと思ったけど、逆にめんどそうなのでcsv
って思ったけど1回の探査で終わらせるには木構造じゃないとダメな気がするのでやはりjson

って思ってたらFileSystemWatcherとかいうクッソ便利なものがあったのでこれを使ってみる
FileSystemWatcher クラス (System.IO) | Microsoft Learn

これ使えばそもそも保存ファイルすら要らないし、unity起動時に
監視先とunity側であるなし食い違いを解消した後
このクラスのイベントを受け取ればええやんってなる、つよい

ただどうやら、サブディレクトリ内の変更がルートで起きた物として解釈されてるようで、
OnCreatedとかでもらえるFullPathにサブディレクトリのパスが含まれてない、なんでやねん

色々調べたらどうもこれ動作が不安定らしいということが見受けられた(ついったーで)
仕方ないので元の方法に戻る
ついでに、そもそも保存ファイル必要ないよねってなったのでまとめる

監視先のファイルが
・unity側にない || 監視先の方が新しい ->監視先のをunityにコピーする
unity側のファイルが
・監視先にない -> unity側のを破棄する

ということをすればいい

できました

コピーや消去時のファイルのハンドル操作が見当たらなかったので若干怖い
まぁ今んとこ動いたのでヨシ!
自動Addressableとの併用確認してないけどまぁいいか!!
併用して動いたのでヨシ!!

github.com